JS 中document.URL 和 windows.location.href 的區(qū)別
更新時間:2009年11月11日 20:09:37 作者:
實際上,document 和 windows 這兩個對象的區(qū)別已經包含了這個問題的答案。
document 表示的是一個文檔對象,windows 表示一個窗口對象。
一個窗口下面可以有很多的document對象。每個document 都有 一個URL。
但是,這不是所有的區(qū)別。當你ctrl + F5 一個鏈接 http://www.dbjr.com.cn/#server
打印 alert(document.URL ); 和 alert(windows.location.href);
發(fā)現,這兩個的值不一樣,
document.URL : http://www.dbjr.com.cn/
windows.location.href :http://www.dbjr.com.cn/#server
所以,如果要用 fragment 進行相應的處理的話,最好是用 windows.location.href
否則會出現很奇怪的錯誤。
一個窗口下面可以有很多的document對象。每個document 都有 一個URL。
但是,這不是所有的區(qū)別。當你ctrl + F5 一個鏈接 http://www.dbjr.com.cn/#server
打印 alert(document.URL ); 和 alert(windows.location.href);
發(fā)現,這兩個的值不一樣,
document.URL : http://www.dbjr.com.cn/
windows.location.href :http://www.dbjr.com.cn/#server
所以,如果要用 fragment 進行相應的處理的話,最好是用 windows.location.href
否則會出現很奇怪的錯誤。