js 如何獲取當(dāng)前url?一起來看看吧!
url是因特網(wǎng)的萬維網(wǎng)服務(wù)程序上用于指定信息位置的表示方法。
Javascript獲取當(dāng)前頁的URL的函數(shù)就是我們經(jīng)常用來重定向的window.location.href。
<script> var url=window.location.href; var loc = url.substring(url.lastIndexOf('/')+1, url.length); alert("url="+url+",loc="+loc); </script>
利用window.location.href的變量獲取整個(gè)url之后,要哪一部分的地址,利用substring,indexof等字符串處理函數(shù)對(duì)獲取到的url進(jìn)行處理,就可以截取你想要的部分。
另外列出一些 javascript 獲取url中各個(gè)部分的功能方法:
返回url 的主機(jī)部分
window.location.host
返回www.xxx.com
window.location.hostname
返回/a/index.php或者/index.php
window.location.pathname
返回url 的協(xié)議部分
window.location.protocol
以上就是小編的分享,希望對(duì)大家有幫助。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com