在 Javascript 中调用外部 HTML 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9685409/
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
Calling external HTML page inside Javascript
提问by vinny
i am trying to call a external HTML page to be displayed on website based on javascript conditions.
我正在尝试根据 javascript 条件调用要在网站上显示的外部 HTML 页面。
The code is like this
代码是这样的
<script language="JavaScript" src="http://j.maxmind.com/app/country.js"></script>
<script type="text/javascript">
var country = geoip_country_code();
if (country == "US")
{
document.write("http://www.mywebsite.com/1.html");
}
else if (country == "GB")
{
document.write("<a href='#'><img src='http://www.image2.com' ><a/>");
}
else
{
document.write("<a href='#'><img src='http://www.image3.com' ><a/>");
}
</script>
Now, instead of showing the content of HTML page to US visitors, it just display "http://www.mywebsite.com/1.html" as plain text.
现在,它不再向美国访问者显示 HTML 页面的内容,而是将“http://www.mywebsite.com/1.html”显示为纯文本。
I am missing a function to call external HTML. Can someone help? Thanks
我缺少调用外部 HTML 的函数。有人可以帮忙吗?谢谢
回答by otakustay
Do you mean the <iframe>
element?
你是说<iframe>
元素吗?
document.write('<iframe src="http://www.mywebsite.com/1.html"></iframe>');
Since <iframe>
cannot resize itself to match the size of its content, be sure to give it a width
/height
attribute or style (if you know the actual size of content).
由于<iframe>
无法调整自身大小以匹配其内容的大小,请务必为其提供width
/height
属性或样式(如果您知道内容的实际大小)。
回答by Diodeus - James MacFarlane
Spitting the text of a URL into a page doesn't magically grab the contents of that page. This type of activity usually happens on the SERVER where your server will fetch the content from another page and serve it up as part of YOUR page. JavaScript is the wrong tool for this job.
将 URL 的文本放入页面并不会神奇地获取该页面的内容。这种类型的活动通常发生在服务器上,您的服务器将从另一个页面获取内容并将其作为您页面的一部分提供。JavaScript 是这项工作的错误工具。
回答by user980058
this kind of thing is really better to do server-side with stuff like php but here's a function I use in a lot of my commercial jobs. Again, I don't condone the use of this function for loading entire pages, but it's a really handy one to have in your toolbox. If anyone says you have to use JQuery to do this, kick them for me. ^_^
这种事情用 php 之类的东西在服务器端做真的更好,但这是我在很多商业工作中使用的一个函数。同样,我不容忍使用此功能加载整个页面,但在您的工具箱中使用它非常方便。如果有人说你必须使用 JQuery 来做到这一点,请帮我踢他们。^_^
function fetchHTML(url)
{
if( 'undefined' == typeof(url) ) return false;
if( document.all ){
p = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
p = new XMLHttpRequest();
}
rnd = Math.random().toString().substring(3);
if( url.indexOf('?') > -1 )
{
url+='&rnd='+rnd;
}
else
{
url+='?rnd='+rnd;
}
p.open("GET",url,false);
p.send(null);
return p.responseText;
}
回答by Ehtesham
well, you are giving a string to document.write()
function, and that's why it is displaying the string that it was supposed to display. If you want to display content of some other page you have two choices either you can use an <iframe>
or use ajax.
好吧,您正在为document.write()
函数提供一个字符串,这就是它显示应该显示的字符串的原因。如果要显示其他页面的内容,您有两种选择,可以使用 an<iframe>
或使用 ajax。