javascript SCRIPT70:添加选项以从最近关闭的 iframe 中选择元素时权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22169779/
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
SCRIPT70: Permission denied when adding options to select element from a recently closed iframe
提问by Lucas Ayala
We are having issues when adding options to a select element from an iframe. The code works well in Chrome, Firefox and Opera, but in IE11 we get a "SCRIPT70: Permision denied" when trying to access the recently created option from the parent window.
向 iframe 中的选择元素添加选项时,我们遇到了问题。该代码在 Chrome、Firefox 和 Opera 中运行良好,但在 IE11 中,当我们尝试从父窗口访问最近创建的选项时,我们会收到“SCRIPT70: Permision denied”。
What we need is to add options to a select element choosing them from a list. The list is shown in a lightbox (with an iframe) and when an element is chosen, it has to be added to the select element and then the lightbox closed (and the iframe destroyed). What we have is something like this (simplified):
我们需要的是向 select 元素添加选项,从列表中选择它们。该列表显示在一个灯箱中(带有 iframe),当一个元素被选中时,它必须被添加到选择元素中,然后灯箱关闭(并且 iframe 被销毁)。我们所拥有的是这样的(简化):
A parent window:
父窗口:
<select id="dropdown">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<!-- in the real code, this iframe is shown when a button is pressed -->
<iframe src="iframe.html"></iframe>
<script>
$(document).ready(function() {
$('#dropdown').on('change', function() {
console.log($(this).children().last().val());
});
});
</script>
And the iframe.html:
和 iframe.html:
<a href="#" data-val="4">Add 4</a>
<a href="#" data-val="5">Add 5</a>
<a href="#" data-val="6">Add 6</a>
<script>
$(document).ready(function() {
$('a').on('click', function() {
var p = window.parent;
if(p) {
var dropdown = p.document.getElementById('dropdown');
if(dropdown) {
var opt = new Option($(this).data('val'), $(this).data('val'));
dropdown.options[dropdown.options.length] = opt;
//close self
$('iframe', p.document).remove();
}
}
});
});
</script>
If you choose any item of the iframe the option is added to the select element and the iframe is destroyed. And now if you access the recently added option in the onchange event attached to the select element you get a "SCRIPT70: Permision denied". And the weirdest thing is that it doesn't happen the first time the event is triggered. You need to trigger the onchange event at least twice to get the error.
如果您选择 iframe 的任何项目,则该选项将添加到 select 元素中,并且 iframe 将被销毁。现在,如果您访问附加到 select 元素的 onchange 事件中最近添加的选项,您将收到“SCRIPT70:权限被拒绝”。最奇怪的是它不会在第一次触发事件时发生。您需要至少触发 onchange 事件两次才能获得错误。
You can see a working fiddle
你可以看到一个工作小提琴
Solution
解决方案
If we change the way the option is added to the select element it works like a charm.
如果我们改变选项添加到 select 元素的方式,它就像一个魅力。
//dropdown.options[dropdown.options.length] = opt;
dropdown.appendChild(opt);
Other scenarios that doesn't raise the error
其他不会引发错误的场景
- If the iframe isn't destroyed, but hidden
- If the option is accessed in an event attached to another object(i.e. onclick event of an anchor)
- 如果 iframe 没有被销毁,而是隐藏了
- 如果在附加到另一个对象的事件中访问该选项(即锚点的 onclick 事件)
The Question
问题
I wish that someone (maybe some Microsoft employee working on the IE team) could explain the details of this strange behaviour.
我希望有人(可能是一些在 IE 团队工作的微软员工)可以解释这种奇怪行为的细节。
回答by user3509208
Try putting this in your html page and see if it makes any difference
试着把它放在你的 html 页面中,看看它是否有什么不同
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>