2011-01-27 26 views
1

我嘗試使用下面的函數來替代DOM樹的每個textNode時:不安全的JavaScript attept訪問幀錯誤在Chrome取代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級象這樣的錯誤(谷歌Chrome瀏覽器,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不會拋出異常。

我該如何解決這個問題?

回答

1

你無法迴避的事實,它不想要做內部框架,那只是事情是這樣的,但是你可以通過更換

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

隨着

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

這樣的作品,但現在這麼想的更換任何東西! – Caipivara 2011-01-27 14:40:28

+0

我可以忽略這個錯誤?活得像它不會發生? – Caipivara 2011-01-27 14:54:35

1

很好地避免錯誤,如果該網站的框架/ iframe從另一個域加載的數據超出了您的javascript代碼所屬的範圍,則會引發異常。

這只是不允許讀取/修改它來自另一個域中的任何數據(見:AJAX cross-domain request,那其實是同一個問題)。

唯一的解決方案是檢查你的循環中的iframe,並且而不是來訪問它們。

相關問題