jQuery 防止 IE 中的“SCRIPT5:访问被拒绝”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17370482/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:55:32  来源:igfitidea点击:

Preventing "SCRIPT5: Access is denied" error in IE

jqueryajaxinternet-explorercross-domain

提问by rinogo

Scenario: Page A on A.com that has an IFrame containing Page B on B.com. Page B uses jQuery 1.10.1 and does notneed to communicate with Page A.

场景:A.com 上的页面 A 具有包含 B.com 上的页面 B 的 IFrame。网页B使用jQuery的1.10.1和它不会需要页次A.沟通

Regardless of this fact, in IE9 and IE10, jQuery generates a "SCRIPT5: Access is denied." error and seemingly refuses to execute any jQuery at all. I have no need of cross-domain communication, AJAX requests, etc., but I do need jQuery to load and execute without errors in Page B.

不管这个事实,在 IE9 和 IE10 中,jQuery 生成一个“SCRIPT5: Access is denied”。错误并且似乎根本拒绝执行任何 jQuery。我不需要跨域通信、AJAX 请求等,但我确实需要 jQuery 在页面 B 中加载和执行而不会出错。

Is there a way to prevent this error from appearing (and inhibiting code execution) in IE9 and IE10? (FYI, other browsers similarly generate "access denied" errors, but they do not hinder code execution)

有没有办法防止这个错误在 IE9 和 IE10 中出现(并禁止代码执行)?(仅供参考,其他浏览器同样会生成“拒绝访问”错误,但它们不会妨碍代码执行)



UPDATE:

更新:

jsFiddle: http://jsfiddle.net/86q5k/4/

jsFiddle:http: //jsfiddle.net/86q5k/4/

The contents of the host page are (from the jsFiddle):

主机页面的内容是(来自jsFiddle):

<iframe src="http://endorkins.com/test-iframe.html"></iframe>

The contents of the iFramed page are:

iFramed 页面的内容是:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            console.log('Hello!  Congratulations.  Your browser is neat, and doesn\'t sniff glue! (http://bit.ly/12QTvTT)');
        });
    </script>
</head>
    <body>
    </body>
</html>

Result in Chrome (Notice the message in the console): enter image description here

结果在 Chrome 中(注意控制台中的消息): 在此处输入图片说明

Result in IE 9.0.8112 (Notice the omission of the message in the console):

结果在 IE 9.0.8112(注意控制台中消息的省略):

enter image description here

在此处输入图片说明

回答by rinogo

I found a workaround. This appears to be a bug ("feature"?) in jQuery 1.10.1. Using jQuery 1.10.0, the error no longer occurs:

我找到了一个解决方法。这似乎是 jQuery 1.10.1 中的一个错误(“功能”?)。使用jQuery 1.10.0,不再出现错误:

http://jsfiddle.net/86q5k/5/

http://jsfiddle.net/86q5k/5/

<iframe src="http://endorkins.com/test-iframe-1.10.0.html"></iframe>

Strange. Very strange. If anyone knows the reason why this is happening in 1.10.1, and how to fix it, I (and jQuery minions around the globe) would certainly be very interested to know! :)

奇怪的。很奇怪。如果有人知道在 1.10.1 中发生这种情况的原因以及如何修复它,我(和全球的 jQuery 奴才)肯定会非常有兴趣知道!:)



UPDATE: Looks like this is a legit jQuery 1.10.1 bug: http://bugs.jquery.com/ticket/13980

更新:看起来这是一个合法的 jQuery 1.10.1 错误:http: //bugs.jquery.com/ticket/13980



UPDATE: According to @emanuele-greco, this is fixed in 1.10.2 and up. So, upgrading your version of jQuery will likely fix the problem.

更新:根据@emanuele-greco,这已在 1.10.2 及更高版本中修复。因此,升级您的 jQuery 版本可能会解决问题。

回答by Dyna Dave Johnson

Ok I Found the same problem and fixed it with the weirdest and most horrible solution ever.

好的,我发现了同样的问题,并用有史以来最奇怪和最可怕的解决方案修复了它。

I'm trying to use an image to popup the file upload dialog, then do an automatic form submit on the change event. I had resigned my self that in IE the users were going to have to click an image to do the post. I placed the image on the form (like i have in several places on the site. This to generated the access denied error, In shock I clicked it again and Yep got the error. On the third click it worked. So I tried it again with the same results first two clicks error and the third worked. And teh simple Javascript code that is ugly as home made soap but now works is

我正在尝试使用图像来弹出文件上传对话框,然后在更改事件上进行自动表单提交。我已经辞职了,在 IE 中,用户将不得不单击图像来发布帖子。我把图片放在表单上(就像我在网站上的几个地方一样。这会产生访问被拒绝的错误,我震惊地再次点击它,是的,得到了​​错误。第三次点击它起作用了。所以我又试了一次结果相同,前两次点击错误,第三次成功。简单的 Javascript 代码像自制肥皂一样丑陋,但现在可以工作了

var files= document.getElementById("newfiles");
files.onchange = addFiles;

function addFiles() {
    try {
        $("#fileupload").submit();
    } catch (e) {
        try {
            $("#fileupload").submit();
        } catch (e) {
            try {
                $("#fileupload").submit();
            } catch (e) {
                alert(e);
            }
        }
    }
};

回答by Brian Ogden

Make sure also that you are not loading your jQuery asset from a CDN such as Google Hosted Libraries. Because the domain is different, that violates certain security protocols that Microsoft uses to protect against Cross Site Scripting in Internet Explorer.

还要确保您没有从 CDN(例如 Google 托管库)加载 jQuery 资产。由于域不同,这违反了 Microsoft 用来防止 Internet Explorer 中的跨站点脚本攻击的某些安全协议。