【问题标题】:Unsafe JavaScript attept to access frame error in Chrome when replacing textNode替换文本节点时,不安全的 JavaScript 尝试访问 Chrome 中的帧错误
【发布时间】:2011-06-16 14:22:43
【问题描述】:

我正在尝试使用以下函数替换 DOM 树的每个 textNode

//Replace each word objective with reposition in each control of the actual jQuery object   
jQuery.fn.replaceEachOne = function (objective, reposition) {
    var regExp = new RegExp('([\\s]'+objective+'[\\s])', "igm");
    this.contents().each(function(){
                if (this.nodeType == 3) {//if is a Node.TEXT_NODE, IE don't have Node object
                    //console.log("pName: "+this.parentNode.nodeName+" pType: "+this.parentNode.nodeType+" Data: " + this.parentNode.data);
                    if(this.data.search(regExp) != -1){
                        var temp = document.createElement("span");

                        temp.innerHTML = this.data.replace(regExp, reposition);

                        //Insert the new one
                        this.parentNode.insertBefore(temp, this);

                        // Remove original text-node:
                        this.parentNode.removeChild(this);
                    }
                }
                else{
                    $(this).replaceEachOne(objective, reposition);
                } 
            }); 
}

它可以工作,但它会抛出 20 个这样的错误(谷歌浏览器,IE 不会抛出):

不安全的 JavaScript 尝试访问 带有 URL 的框架 http://cdn.apture.com/media/html/aptureLoadIframe.html?v=21872561 从带有 URL 的框架 http://c-jfmunoz:5000/SitePages/Home.aspx。 域、协议和端口必须 匹配。

进行一些调试,我发现它在将 textnode 插入 Web 表单时引发了异常。

我必须将此 JavaScript 附加到 Sharepoint 2010 站点。在本地查看时,Chrome 不会引发异常。

我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery dom google-chrome


    【解决方案1】:

    你无法绕过它不想做 iframe 的事实,就是这样,但你可以通过替换来避免错误

     this.contents().each(function(){
    

     this.contents().not('iframe').each(function(){
    

    【讨论】:

      【解决方案2】:

      好吧,如果网站的框架/iframe 从另一个域加载数据,而不是您的 javascript 代码所属的域,这将引发异常。

      只是不允许读取/修改来自另一个域的任何数据(参见:AJAX cross-domain request,这实际上是同一个问题)。

      唯一的解决方案是检查循环中的 iframe, 访问这些。

      【讨论】:

      • 但是如果我需要完成工作的页面是从另一个域加载的?在 sharepoint 中,有一个母版页,它会在那里加载其余的“子页”(但我认为它们属于同一个域)。
      • 我认为那里的所有内容都加载到一个或多个 iframe 中。我能做什么?
      • @Daniel:如果确实是不同的 iframe,请检查每个 iframe 的 src 属性。如果域在某处有所不同,那么您实际上无能为力。您无权通过脚本访问它们。
      • 但是,如果我留下了这个错误,我能活得没有任何问题吗?
      猜你喜欢
      • 2013-04-19
      • 2012-05-26
      • 2011-08-05
      • 1970-01-01
      • 2011-09-25
      • 2012-12-07
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多