//獲取當(dāng)前文件全路徑
<script language= "javascript" >
alert(window.location.href); //
alert(window.location); //
alert(location.href); //
alert(parent.location.href); //
alert(top.location.href);
alert(document.location.href);
alert(document.URL);
</script>
//獲取當(dāng)前目錄方法
<script type= "text/javascript" >
//方法一
var str = location.href;
var arr = str.split( "/" );
delete arr[arr.length-1];
var dir = arr.join( "/" );
alert(dir);
//方法二
alert(location.href.substring(0,location.href.lastIndexOf( '/' )));
</script>
//獲取當(dāng)前文件名
<script language=javascript>
var filename=location.href;
filename=filename.substr(filename.lastIndexOf( '/' )+1);
alert(filename);
</script>
如何用js得到當(dāng)前頁面的url信息方法(JS獲取當(dāng)前網(wǎng)址信息)
設(shè)置或獲取對象指定的文件名或路徑。
alert(window.location.pathname)
設(shè)置或獲取整個 URL 為字符串。
alert(window.location.href);
設(shè)置或獲取與 URL 關(guān)聯(lián)的端口號碼。
alert(window.location.port)
設(shè)置或獲取 URL 的協(xié)議部分。
alert(window.location.protocol)
設(shè)置或獲取 href 屬性中在井號“ #”后面的分段。
alert(window.location.hash)
設(shè)置或獲取 location 或 URL 的 hostname 和 port 號碼。
alert(window.location.host)
設(shè)置或獲取 href 屬性中跟在問號后面的部分。
alert(window.location.search)
獲取變量的值(截取等號后面的部分)
var url = window.location.search;
alert(url.length);
alert(url.lastIndexOf( '=' ));
var loc = url.substring(url.lastIndexOf( '=' )+1, url.length);
JS獲取當(dāng)前網(wǎng)址,JS獲取當(dāng)前域名URL ,JS獲取網(wǎng)站完整路徑頁面地址
1.獲取當(dāng)前完整網(wǎng)址
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = " thisURL: [" + thisURL + "]<br />"
strwrite += " thisHREF: [" + thisHREF + "]<br />"
strwrite += " thisSLoc: [" + thisSLoc + "]<br />"
strwrite += " thisDLoc: [" + thisDLoc + "]<br />"
document.write( strwrite );
2.獲取當(dāng)前域名信息
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = " thisTLoc: [" + thisTLoc + "]<br />"
strwrite += " thisPLoc: [" + thisPLoc + "]<br />"
strwrite += " thisTHost: [" + thisTHost + "]<br />"
strwrite += " thisHost: [" + thisHost + "]<br />"
document.write( strwrite );
3.獲取當(dāng)前頁面
tmpHPage = thisHREF.split( "/" );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( "/" );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = " thisHPage: [" + thisHPage + "]<br />"
strwrite += " thisUPage: [" + thisUPage + "]<br />"
document.write( strwrite );
|