ie6,7 location.href 沒有權限的出現原因及解決方案


     QA 給我提了個bug,說是頁面在ie7下點擊切換語言報了個js錯誤。於是在辦公網配上開發機host便用ie8的ie7模式看了下,一切正常,到qa同事機器看了下,確 實報js錯誤。。。看來只有純ie7才會有這個問題。回到座位開啟虛擬機,用ie7試了下首頁,果然可以重現, permission declined。以下是切換語言代碼:      
if (lang && lang !== __Config.current_lang && lists[lang]){  
        var reg_lang = /(&|\?)(lang=)[^&]*(&|$)/g;  
       location.href = (location.href.replace(reg_lang, '$1').replace(/#.*$/,'') + '&lang=' + lang).replace(/&+/g, '&').replace(/\/&/,'/?');  
    }   
       看了下唯一可能出問題的就是location.href這里,去掉 location.href = (location.href.replace(reg_lang, '$1').replace(/#.*$/,'') + '&lang=' + lang).replace(/&+/g, '&').replace(/\/&/,'/?');果然沒有報錯了。這就奇怪了,首頁就在頂級域名xx .com 下,沒有任何跨域的可能。於是新建了一個測試頁面把代碼貼進去,一切正常。。。那究竟是哪里出問題呢,回頭繼續看代碼,首頁為了增加feedback,弄了個iframe把feedback.xx.com下一個頁面加載進來,而該頁面在提交feedback成功后會調用父頁面的關閉彈出層方法,由於跨域原因父頁面與該子頁面都加入了document.domain='xx.com'這句,而這句正是與測試頁面不同的地方,於是乎在首頁試着把 document.domain='xx.com'去掉,語言切換正常了,但feedbak子頁面提交后彈出層不能關閉了,由於跨域。猜想在測試頁面加上document.domain='xx.com'應該也會報沒權限吧,試了下結果竟然還是正常。。。還有什么不同???難道是jquery?測試頁面沒有引入,抱着試試看的態度把jquery 1.71加進去,wow,測試頁面終於報錯了。坑爹啊,jquery究竟做了什么?網上找了幾乎沒啥資料,總不能讓我把jquery從首頁去掉吧,實在沒有頭緒,這個切換語言必須要獲取當前頁面url才能做。接着再試,發現在主頁設置documen.domain之前是可以獲取到location.href的,這樣看起來還不錯,那在設置domain后不能讀,是否可以設置呢?試了下ok,這樣的話不完美解決方案產生了,在設置domain前先用個變量存儲當前頁面url,在設置的時候直接用,這樣可以避開ie 7 location.href的讀取權限問題。

 

       想了下如果頁面的hash變了,而hash的改變並不會刷新頁面,這樣我之前獲取的url就不准確了,腫么辦?不知道,郁悶中抄起一根精白沙走到了吸煙區,點上,慢慢踱步,是不是還有其他方法獲取當前頁面url呢?突然靈光一現,document.URL是不是可以呢?狠狠的掐滅煙頭,奔回座位在測試頁面試了下沒報腳本錯誤,順利取到了當前頁面url。

      總結如下:
      在ie6,7頁面下如果設置domain如與當前域相同,且引入了jquery(我試了1.4.2, 1.7.1, 1.8.1)取location.href會報沒有權限的錯誤。

      解決方案:
      用documen.URL代替(document.URL 取值時等價於 window.location.href 或 document.location.href。在某些瀏覽器中通過對 document.URL 賦值來實現頁面跳轉,但某些瀏覽器中不行)。
 

最后附上幾段測試代碼,測試環境虛擬機中,ie6,7

成功,設置domain,引入jquery,用document.URL,用document.URL在下面幾種情況都ok;
< html >  
< head >  
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />  
</ head >  
< body >  
< input  id ="test"  type ="button"  value ="test"  onclick ="test()"   />  
< script  src ="http://www.xx.com/wechat_portal/js/jquery.js" ></ script >   
< script >   
document.domain 
=   " xx.com " ;  
     
function  test() {     
       document.getElementById(
' test ' ).value  =  document.URL;  
    }  
  
</ script >  
</ body >  
</ html > 


失敗, 設置domain,引入jquery,用location.href;
< html >  
< head >  
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />  
</ head >  
< body >  
< input  id ="test"  type ="button"  value ="test"  onclick ="test()"   />  
< script  src ="http://www.xx.com/wechat_portal/js/jquery.js" ></ script >   
< script >   
document.domain 
=   " xx.com " ;  
     
function  test() {     
       document.getElementById(
' test ' ).value  =  location.href;  
    }  
  
</ script >  
</ body >  
</ html > 

 

成功,設置domain,不引入jquery,用location.href;
< html >  
< head >  
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />  
</ head >  
< body >  
< input  id ="test"  type ="button"  value ="test"  onclick ="test()"   />    
< script >   
document.domain 
=   " xx.com " ;  
     
function  test() {     
       document.getElementById(
' test ' ).value  =  location.href;  
    } 
</ script >  
</ body >  
</ html >
 

成功,不設置domain引入jquery,用location.href;

< html >  
< head >  
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />  
</ head >  
< body >  
< input  id ="test"  type ="button"  value ="test"  onclick ="test()"   />  
< script  src ="http://www.xx.com/js/jquery.js" ></ script >      
< script >   
     
function  test() {     
       document.getElementById(
' test ' ).value  =  location.href;  
    } 
</ script >  
</ body >  
</ html > 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM