Javascript 将外部页面 html 插入页面 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4967629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert external page html into a page html
提问by Zitun
I'd like to load/insert an external html page into my web page. Example :
我想将外部 html 页面加载/插入到我的网页中。例子 :
<b>Hello this is my webpage</b>
You can see here an interresting information :
XXXX
Hope you enjoyed
the XXXXshould be replaced by a small script (the smaller as possible) that load a page like http://www.mySite.com/myPageToInsert.html
的XXXX应该由一个小的脚本(尽可能小,因为),该负载像页替换http://www.mySite.com/myPageToInsert.html
I found the following code with jquery :
我用 jquery 找到了以下代码:
<script>$("#testLoad").load("http://www.mySite.com/myPageToInsert.html");</script>
<div id="testLoad"></div>
I would like the same without using an external javascript library as jquery...
我希望不使用外部 javascript 库作为 jquery ......
回答by zozo
There are 2 solutions for this (2 that I know at least):
对此有 2 个解决方案(我至少知道 2 个):
Iframe -> this one is not so recommended
Send an ajax request to the desired page.
iframe -> 这个不太推荐
向所需页面发送 ajax 请求。
Here is a small script:
这是一个小脚本:
<script type="text/javascript">
function createRequestObject() {
var obj;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
obj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
obj = new XMLHttpRequest();
}
return obj;
}
function sendReq(req) {
var http = createRequestObject();
http.open('get', req);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if (http.readyState == 4) {
var response = http.responseText;
document.getElementById('setADivWithAnIDWhereYouWantIt').innerHTML=response;
}
}
sendReq('yourpage');
//previously </script> was not visible
</script>
回答by Phil Jenkins
Would an iframefit the bill?
将一个IFRAME符合该法案?
<b>Hello this is my webpage</b>
You can see here an interresting information :
<iframe id="extFrame" src="http://www.mySite.com/myPageToInsert.html"></iframe>
Hope you enjoyed
You can set the src
attribute of your iframe element using plain old javascript to switch out the page for another
您可以src
使用普通的旧 javascript设置iframe 元素的属性来切换另一个页面
回答by VP.
I think what you are looking for are in the Jquery source code.
我认为您正在寻找的是 Jquery 源代码。
you can see more details here $(document).ready equivalent without jQuery
您可以在此处查看更多详细信息$(document).ready 等价物,无需 jQuery