主要用到Location 對象,包含有關當前 URL 的信息,是 Window 對象的一個部分,可通過 window.location 屬性來訪問。
方法一 (window.document.location.href/window.document.location.pathname) ------------轉自網絡
方法二(window.location.pathname/window.location.protocol/window.location.host)
function getRootPath_dc() {
var pathName = window.location.pathname.substring(1);
var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
if (webName == "") {
return window.location.protocol + '//' + window.location.host;
}
else {
return window.location.protocol + '//' + window.location.host + '/' + webName;
}
}
注:
1、document默示的是一個文檔對象,window默示的是一個窗口對象,一個窗口下可以有多個文檔對象。
所以一個窗口下只有一個window.location.href,然則可能有多個document.URL、document.location.href------------轉自網絡
2、window.location.href和document.location.href可以被賦值,然後跳轉到其它頁面,document.URL只能讀不可以寫------------轉自網絡
3、Location 對象詳細信息參考w3school http://www.w3school.com.cn/jsref/dom_obj_location.asp