最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I load a webpage into a <div> element? - Stack Overflow

programmeradmin3浏览0评论

I'm quite new to Javascript and I have a question about loading a webpage into a div element ('content-window') on my website. I want to open a webpage in that div, in this case . I've to write the following line in order to select the div element.

var sidediv = document.getElementById('content-window');

Can someone tell me how can I load a webpage into this side div? I've tried a lot (but I've failed of course). For example, the following line won't work:

sidediv.location.assign = "";

Thanks in advance!

I'm quite new to Javascript and I have a question about loading a webpage into a div element ('content-window') on my website. I want to open a webpage in that div, in this case http://www.fdsa.?ID=1. I've to write the following line in order to select the div element.

var sidediv = document.getElementById('content-window');

Can someone tell me how can I load a webpage into this side div? I've tried a lot (but I've failed of course). For example, the following line won't work:

sidediv.location.assign = "http://www.fdsa.?ID=1";

Thanks in advance!

Share Improve this question edited Feb 14, 2015 at 15:35 ilgaar 8441 gold badge12 silver badges33 bronze badges asked Feb 14, 2015 at 14:25 RobinhoRobinho 451 silver badge7 bronze badges 2
  • 1 possible duplicate of How do I load an HTML page in a <div> using JavaScript? – Chad W Commented Feb 14, 2015 at 14:34
  • 1 you could use an i frame – Dayan Moreno Leon Commented Feb 14, 2015 at 14:50
Add a ment  | 

3 Answers 3

Reset to default 5

you should use an <iframe> tag as follows:

<div>
  <iframe src="http://www.fdsa.?ID=1">
</div>

or build it programatically:

document.getElementById("content-window").innerHTML='<iframe src="http://www.fdsa.?ID=1">'

you could use more <iframe> information here: https://developer.mozilla/en/docs/Web/HTML/Element/iframe

enjoy!

HTMLCODE :

  <body onload="loadHtml()">
    <div id="content-window"></div>
  </body>

CSS CODE :

#content-window {
    width: 100%;

}

#content-window object{
    width: 100%;

}

JAVASCRIPT :

function loadHtml() {
    document.getElementById("content-window").innerHTML='<object type="text/html" data="http://www.fdsa.?ID=1"></object>';
}

Another approach is AJAX, if you don't want it embeded inside and rather actually inline as part of the document.

The easiest (and for the sake of convenience, I will post it here) method utilizes jQuery, it's so easy it's incredible, obviously there are other methods but I have to post this one (I use it everywhere):

Here is the function:

.load("http://url./")

发布评论

评论列表(0)

  1. 暂无评论