SCRIPT70:添加选项以从最近关闭的iframe中选择元素时,权限被拒绝 [英] SCRIPT70: Permission denied when adding options to select element from a recently closed iframe

查看:86
本文介绍了SCRIPT70:添加选项以从最近关闭的iframe中选择元素时,权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iframe向select元素添加选项时遇到问题。该代码在Chrome,Firefox和Opera中运行良好,但在IE11中,当试图从父窗口访问最近创建的选项时,我们会看到SCRIPT70:Permision denied。

$ b

我们需要的是将选项添加到从列表中选择它们的select元素。该列表显示在一个灯箱(带有一个iframe)中,当选择一个元素时,必须将它添加到select元素,然后关闭lightbox(并将iframe销毁)。我们有什么是这样的(简化):

一个父窗口:

 < select id =dropdown> 
< option> 1< / option>
< option> 2< / option>
< option> 3< / option>
< / select>

<! - - 在真实代码中,当按下按钮时显示此iframe - >
< iframe src =iframe.html>< / iframe>
< script> $($'$')$($'$'$'$'$'$'$'$'$'$' children()。last()。val());
});
});
< / script>

以及iframe.html:

 < a href =#data-val =4>添加4< / a> 
< a href =#data-val =5>添加5< / a>
< a href =#data-val =6>添加6< / a>
< script> ('click',function(){
var p = window.parent; $ b $(
$(document).ready(function(){
$ (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;
//关闭自我
$(' iframe',p.document).remove();
}
}
});
});
< / script>

如果您选择iframe的任何项目,则将选项添加到select元素并且iframe被销毁。而现在,如果您访问附加到select元素的onchange事件中最近添加的选项,则会显示SCRIPT70:Permision denied。最奇怪的是,事件第一次触发时不会发生。您需要触发onchange事件至少两次以获取错误



您可以看到工作小提琴

解决方案



如果我们改变选项添加到select元素的方式它就像一个魅力

  // dropdown.options [dropdown.options.length] = opt; 
dropdown.appendChild(opt);



不会引发错误的其他方案



问题



我希望有人(也许一些在IE团队工作的微软员工)能够解释这种奇怪行为的细节。

尝试把它放在你的html页面,看看它是否有任何区别。

 < meta http-equiv =X-UA-兼容content =IE = edge,chrome = 1/> 


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.

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):

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:

<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.

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.

//dropdown.options[dropdown.options.length] = opt;
dropdown.appendChild(opt);

Other scenarios that doesn't raise the error

The Question

I wish that someone (maybe some Microsoft employee working on the IE team) could explain the details of this strange behaviour.

解决方案

Try putting this in your html page and see if it makes any difference

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>

这篇关于SCRIPT70:添加选项以从最近关闭的iframe中选择元素时,权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆