jQuery跨域iframe脚本 [英] jQuery cross domain iframe scripting
问题描述
我尝试访问加载外部网址的iframe的DOM。当然,我得到一个Permission denied for错误,由于跨域安全。如何使这项工作?我看到用json做的事情(但我不能从外部源获得一个json字符串)和用HTML5 postmessage做的事情。
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.
你可以看到它生活在:
http://jsfiddle.net/QPBvJ/
you can see it live at : http://jsfiddle.net/QPBvJ/
代码是:
$(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
推荐答案
没有办法让这项工作。除非您尝试访问的外部域支持这样的过程CORS , JSONP 或 postMessage 。
There is no way to make this work. Unless, the foreign domain you try to access supports a procedure like C.O.R.S, JSONP or postMessage.
有一些例外(像往常一样):
There are a few exceptions (like always):
例如,如果你处理一个WebApp,你可以告诉你的用户他们必须授予跨域调用
。
If you're dealing with a WebApp for instance, you can tell your users that they have to grant access to cross-domain-calls
.
在Gecko / Firefox中,您可以调用
In Gecko/Firefox for instance, you can call
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead')
它允许浏览器通过 ajax / iframes
访问外部域。在这种情况下,用户必须设置
which enables the browser to access foreign domains via ajax/iframes
. In this scenario, an user has to set
signed.applets.codebase_principal_support
至 true
在 about:config
在这个世界的 Internet Explorers
中,有一个名为允许跨域访问
深深隐藏在安全
选项卡中,必须设置为 enable
。
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
.
Chrome
允许使用命令行参数跨域调用:
Chrome
allows cross-domain calls with a commandline argument:
chrome.exe --disable-web-security
这篇关于jQuery跨域iframe脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!