javascript - 替换文本节点时,不安全的 JavaScript 尝试访问 Chrome 中的框架错误

标签 javascript jquery dom google-chrome

我正在尝试使用以下函数替换 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 个这样的错误(Google Chrome,IE 不会抛出):

Unsafe JavaScript attempt to access frame with URL http://cdn.apture.com/media/html/aptureLoadIframe.html?v=21872561 from frame with URL http://c-jfmunoz:5000/SitePages/Home.aspx. Domains, protocols and ports must match.

进行一些调试后,我发现它在将文本节点插入 Web 表单时抛出异常。

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

我该如何解决这个问题?

最佳答案

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

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

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

关于javascript - 替换文本节点时,不安全的 JavaScript 尝试访问 Chrome 中的框架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817599/

相关文章:

javascript - Service Worker 注册问题

javascript - onclick 事件未在 HTML 上运行选择下拉

jquery - CSS3 渐变和背景图片

jQuery Cycle插件z-index float 问题

javascript - Vue 在删除 DOM 元素后将内联样式传输到下一个元素 [with snippet]

javascript - 如何用for循环填充数组?

javascript - 有没有办法在 html 中使用 JavaScript 检测溢出?

javascript - 使用 Jquery 或 JavaScript 需要字符串的特定部分吗?

html - Chrome 不显示 div

javascript - 如何在没有 id 的情况下访问 canvas 元素?