jQuery 跨域 iframe 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3083112/
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
jQuery cross domain iframe scripting
提问by Mircea
I am trying to access the DOM of an iframe that loads an external URL. Of course that I get a "Permission denied for" error due to cross domain security. How can I make this work? I saw something done with json (but I can not get a json string from my external source) and something done with HTML5 postmessage.
我正在尝试访问加载外部 URL 的 iframe 的 DOM。当然,由于跨域安全,我收到“权限被拒绝”错误。我怎样才能使这项工作?我看到了一些用 json 完成的东西(但我无法从我的外部源获取 json 字符串)和一些用 HTML5 postmessage 完成的东西。
you can see it live at : http://jsfiddle.net/QPBvJ/
您可以在以下位置实时查看:http: //jsfiddle.net/QPBvJ/
The code is:
代码是:
$(document).ready(function(){
$('#get').live('click', function() {
var currentIFrame = $('#frameDemo');
currentIFrame.contents().find("a").css("background-color","#BADA55");
alert ("done")
});
});
<iframe src="http://api.jquery.com/" width="80%" height="600" id='frameDemo'></iframe>
<button id="get">Get</button>
What would the the easiest way to make this work. Thank you
使这项工作的最简单方法是什么。谢谢
回答by jAndy
There is no way to make this work. Unless, the foreign domain you try to access supports a procedure like C.O.R.S, JSONPor postMessage.
没有办法使这项工作。除非,您尝试访问的外部域支持诸如CORS、JSONP或postMessage 之类的过程。
There are a few exceptions (like always):
有一些例外(一如既往):
If you're dealing with a WebApp for instance, you can tell your users that they have to grant access to cross-domain-calls
.
例如,如果您正在处理一个 WebApp,您可以告诉您的用户他们必须授予对cross-domain-calls
.
In Gecko/Firefox for instance, you can call
例如,在 Gecko/Firefox 中,您可以调用
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead')
which enables the browser to access foreign domains via ajax/iframes
. In this scenario, an user has to set
这使浏览器能够通过ajax/iframes
. 在这种情况下,用户必须设置
signed.applets.codebase_principal_support
to true
under about:config
to make this work.
到true
下about:config
,使这项工作。
In the Internet Explorers
of this world, there is a setting called something like allow cross-domain access
deeply hidden in the security
tab, which must be set to enable
.
在Internet Explorers
这个世界上,有一个叫做allow cross-domain access
深藏在security
标签页中的东西,必须设置为enable
。
Chrome
allows cross-domain calls with a commandline argument:
Chrome
允许使用命令行参数进行跨域调用:
chrome.exe --disable-web-security