开发者

Unsafe JavaScript attept to access frame error in Chrome when replacing textNode

I'm trying to replace each textNode of the DOM tree using the following function:

//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);
                } 
            }); 
}

It works but it throws 20 errors like this (Google Chrome, IE don't throws):

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/Sit开发者_StackOverflowePages/Home.aspx. Domains, protocols and ports must match.

Doing some debugging I look that it throws the exception when the textnode is being inserted into a web form.

I have to attach this JavaScript to a Sharepoint 2010 site. When viewed locally Chrome doesn't throw the exception.

How can I fix this?


You cant get around the fact that it dont want to do iframes, thats just the way it is, but you can avoid the errors by replacing

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

With

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


Well, if the site has frames/iframes which load data from another domain than your javascript code belongs too, this will throw an exception.

It's just not allowed to read/modify any data which comes from another domain (see: AJAX cross-domain request, that is actually the same issue).

The only solution is to check for iframes in your loop and not to access those.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜