QA 給我提了個(gè)bug,說(shuō)是頁(yè)面在ie7下點(diǎn)擊切換語(yǔ)言報(bào)了個(gè)js錯(cuò)誤。于是在辦公網(wǎng)配上開發(fā)機(jī)host便用ie8的ie7模式看了下,一切正常,到qa同事機(jī)器看了下,確 實(shí)報(bào)js錯(cuò)誤。。。看來(lái)只有純ie7才會(huì)有這個(gè)問(wèn)題。回到座位開啟虛擬機(jī),用ie7試了下首頁(yè),果然可以重現(xiàn), permission declined。以下是切換語(yǔ)言代碼:
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(/\/&/,'/?');
}
看了下唯一可能出問(wèn)題的就是location.href這里,去掉location.href = (location.href.replace(reg_lang, '$1').replace(/#.*$/,'') + '&lang=' + lang).replace(/&+/g, '&').replace(/\/&/,'/?');果然沒有報(bào)錯(cuò)了。這就奇怪了,首頁(yè)就在頂級(jí)域名xx.com下,沒有任何跨域的可能。于是新建了一個(gè)測(cè)試頁(yè)面把代碼貼進(jìn)去,一切正常。。。那究竟是哪里出問(wèn)題呢,回頭繼續(xù)看代碼,首頁(yè)為了增加feedback,弄了個(gè)iframe把feedback.xx.com下一個(gè)頁(yè)面加載進(jìn)來(lái),而該頁(yè)面在提交feedback成功后會(huì)調(diào)用父頁(yè)面的關(guān)閉彈出層方法,由于跨域原因父頁(yè)面與該子頁(yè)面都加入了document.domain='xx.com'這句,而這句正是與測(cè)試頁(yè)面不同的地方,于是乎在首頁(yè)試著把document.domain='xx.com'去掉,語(yǔ)言切換正常了,但feedbak子頁(yè)面提交后彈出層不能關(guān)閉了,由于跨域。猜想在測(cè)試頁(yè)面加上document.domain='xx.com'應(yīng)該也會(huì)報(bào)沒權(quán)限吧,試了下結(jié)果竟然還是正常。。。還有什么不同???難道是jquery?測(cè)試頁(yè)面沒有引入,抱著試試看的態(tài)度把jquery 1.71加進(jìn)去,wow,測(cè)試頁(yè)面終于報(bào)錯(cuò)了?拥,jquery究竟做了什么?網(wǎng)上找了幾乎沒啥資料,總不能讓我把jquery從首頁(yè)去掉吧,實(shí)在沒有頭緒,這個(gè)切換語(yǔ)言必須要獲取當(dāng)前頁(yè)面url才能做。接著再試,發(fā)現(xiàn)在主頁(yè)設(shè)置documen.domain之前是可以獲取到location.href的,這樣看起來(lái)還不錯(cuò),那在設(shè)置domain后不能讀,是否可以設(shè)置呢?試了下ok,這樣的話不完美解決方案產(chǎn)生了,在設(shè)置domain前先用個(gè)變量存儲(chǔ)當(dāng)前頁(yè)面url,在設(shè)置的時(shí)候直接用,這樣可以避開ie 7 location.href的讀取權(quán)限問(wèn)題。
想了下如果頁(yè)面的hash變了,而hash的改變并不會(huì)刷新頁(yè)面,這樣我之前獲取的url就不準(zhǔn)確了,腫么辦?不知道,郁悶中抄起一根精白沙走到了吸煙區(qū),點(diǎn)上,慢慢踱步,是不是還有其他方法獲取當(dāng)前頁(yè)面url呢?突然靈光一現(xiàn),document.URL是不是可以呢?狠狠的掐滅煙頭,奔回座位在測(cè)試頁(yè)面試了下沒報(bào)腳本錯(cuò)誤,順利取到了當(dāng)前頁(yè)面url。
總結(jié)如下:
在ie6,7頁(yè)面下如果設(shè)置domain如與當(dāng)前域相同,且引入了jquery(我試了1.4.2, 1.7.1, 1.8.1)取location.href會(huì)報(bào)沒有權(quán)限的錯(cuò)誤。
解決方案:
用documen.URL代替(document.URL 取值時(shí)等價(jià)于 window.location.href 或 document.location.href。在某些瀏覽器中通過(guò)對(duì) document.URL 賦值來(lái)實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),但某些瀏覽器中不行)。
最后附上幾段測(cè)試代碼,測(cè)試環(huán)境虛擬機(jī)中,ie6,7
成功,設(shè)置domain,引入jquery,用document.URL,用document.URL在下面幾種情況都o(jì)k;
<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>
失敗, 設(shè)置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>
成功,設(shè)置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>
成功,不設(shè)置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>