location.pathname: 返回URL的網域名稱(網域名稱IP)後的部分。
例如 http://www.example.com/wordpress/返回/wordpress/,
又或則 http://127.0.0.1/index.html 返回/index.html,
注意是帶url的網域名稱或網域名稱IP
在磁碟上隨便建個Html檔案進行location.pathname測試,如瀏覽器上的路徑是: C:\Documents and Settings\Administrator\案頭\testjs.html, 這樣,得到的結果是: /C:\Documents and Settings\Administrator\案頭\testjs.html
既然提到這了,那我們就分析下下面的URL:
http://www.example.com:8080/test.php?user=admin&pwd=admin#login
想得到整個如上的完整url,我們用:location.href;
得到傳輸協議http:,我們用:location.protocol;
得到主機名稱連同連接埠www.example.com:8080,我們用:location.host;
得到主機名稱www.joymood.cn,我們用:location.hostname;
得到主機後部分不包括問號?後部分的/test.php,就用我們剛才講的:location.pathname;
得到url中問號?之後井號#之前的部分?user=admin&pwd=admin,我們就用: location.search;
得到#之前的部分#login,我們就用location.hash
如上,我們可以通過location對象的某些屬性得到一個完整URL的各個部分。
http://hi.baidu.com/xiaoxie336l/item/8628a634fdc3a5d996f88d70
this.getContextPath = function() {
var path;
if(location.pathname.indexOf("/")==0){
path=location.pathname.substring(1);
}else{
path=location.pathname;
}
var index=path.indexOf("/");
var contextPath="/"+path.substring(0,index);
return contextPath;
}