简体   繁体   English

如果我需要将外部网址加载到网页中,是否可以使用iframe替代

[英]Are there any alternatives for an IFrame if I need to load an external url to my webpage

I need to know if there are any alternatives for an IFrame that I could use to load in an external URL into my webpage. 我需要知道是否有IFrame的替代方案,可以用来将外部URL加载到我的网页中。 Even if it's JQuery or JavaScript or PHP 即使是JQuery或JavaScript或PHP

You define a container in your page such as: 您在页面中定义一个容器,例如:

<div id="external"></div>

then you fill it with an AJAX call via jQuery: 然后通过jQuery通过AJAX调用将其填充:

$('#external').load("http://remote.page/");

EDIT: as pointed out by Graza in the comment, this won't work if the external page is on another domain due to the limitation on XMLHttpRequest objects -- if you need that you'll have to do a JSONP request, which requires cooperation on the server side. 编辑:正如Graza在评论中所指出的,由于XMLHttpRequest对象的限制,如果外部页面在另一个域上,则此方法将不起作用-如果您需要执行JSONP请求,则需要进行合作在服务器端。 See this article for more information. 有关更多信息,请参见本文

Be so careful doing this though, make sure you absolutely trust the 3rd party site. 不过请务必小心,请确保您绝对信任第三方网站。 If they inject javascript into your page, it will be running under the same context as your own site, which could lead to all sorts of hackery followed by only tears and sadness. 如果他们将javascript注入您的页面,它将在与您自己的网站相同的上下文中运行,这可能会导致各种各样的黑客行为,而后只会流下眼泪和悲伤。 IFrames give you a certain amount of protection for this. iFrame为此提供了一定程度的保护。

Use .load() 使用.load()

Example: 例:

$("#feeds").load("feeds.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM