Ajax/jQuery - 在页面加载时将网页内容加载到 div 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9963799/
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
Ajax/jQuery - Load webpage content into a div on page load?
提问by CodeyMonkey
Without using iframes, is it possible to load the contents of
不使用iframes,是否可以加载内容
<div id="siteloader"></div>
With an external site e.g. somesitehere.com
使用外部站点,例如 somesitehere.com
When the page loads? - I know how to load contents from a file, but wasn't sure how to load a whole site?
页面加载时?- 我知道如何从文件加载内容,但不确定如何加载整个站点?
Many thanks,
非常感谢,
回答by Marcel
This is possible to do without an iframe
specifically. jQuery is utilised since it's mentioned in the title.
这可以在没有iframe
特定的情况下进行。jQuery 被使用,因为它在标题中提到。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Load remote content into object element</title>
</head>
<body>
<div id="siteloader"></div>?
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$("#siteloader").html('<object data="http://tired.com/">');
</script>
</body>
</html>
回答by faino
Take a look into jQuery's .load()function:
看看jQuery 的 .load()函数:
<script>
$(function(){
$('#siteloader').load('http://www.somesitehere.com');
});
</script>
However, this only works on the same domain of the JS file.
但是,这只适用于 JS 文件的同一域。
回答by Njdart
With jQuery, it is possible, however not using ajax.
使用 jQuery,这是可能的,但不能使用 ajax。
function LoadPage(){
$.get('http://a_site.com/a_page.html', function(data) {
$('#siteloader').html(data);
});
}
And then place onload="LoadPage()"
in the body tag.
然后放入onload="LoadPage()"
body标签中。
Although if you follow this route, a php version might be better:
虽然如果你遵循这条路线,一个 php 版本可能会更好:
echo htmlspecialchars(file_get_contents("some URL"));
回答by Lee Davis
You can't inject content from another site (domain) using AJAX. The reason an iFrame is suited for these kinds of things is that you can specify the source to be from another domain.
您不能使用 AJAX 从另一个站点(域)注入内容。iFrame 适用于这些类型的原因是您可以指定来自另一个域的源。