location對(duì)象的屬性和方法應(yīng)用(解析URL)
更新時(shí)間:2013年04月12日 17:22:26 作者:
本文將與大家分享下location對(duì)象使用屬性和方法來(lái)解析URL的實(shí)例,感興趣的朋友可以參考下,希望對(duì)你有所幫助
location對(duì)象提供了很多屬性和方法用來(lái)解析URL。
<head>
<title></title>
<script type="text/javascript">
var uristr = window.location.search.substr(1);
var array = uristr.split('&&');
for (var i = 0; i < array.length; i++) {
var array1 = array[i].split('=');
alert(array1[0]);
}
--------------------//hash:返回#符號(hào)后的內(nèi)容
function showhash() {
alert(window.location.hash);
}
--------------------//host:服務(wù)器的名字
function showhost() {
alert(window.location.host);
}
--------------------//href:當(dāng)前載入的頁(yè)面的完整的URL
function showhref() {
alert(window.location.href);
}
--------------------//pathname:url中主機(jī)名后的部分
function showpathname() {
alert(window.location.pathname);
}
--------------------//protocal:URL中使用的協(xié)議
function showprotacal() {
alert(window.location.protocal);
}
--------------------//search:執(zhí)行g(shù)et請(qǐng)求的URL中問(wèn)號(hào)后面的部分,又稱(chēng)為查詢(xún)字符串
function showsearch() {
alert(window.location.search);
}
</script>
</head>
<body>
<input type="button" value="Hash" onclick="showhash();" />
<br />
<input type="button" value="host" onclick="showhost();" />
<br />
<input type="button" value="href" onclick="showhref();" />
<br />
<input type="button" value="pathname" onclick="showpathname();" />
<br />
<input type="button" value="protocal" onclick="showprotacal();" />
<br />
<input type="button" value="search" onclick="showsearch();" />
</body>
測(cè)試search的時(shí)候,需要從另一個(gè)頁(yè)面點(diǎn)擊一個(gè)連接,從瀏覽器地址欄穿過(guò)來(lái)值:
<body>
<a href="HTMLPage1.htm?name='王五'&&age=22">GO</a>
</body>
復(fù)制代碼 代碼如下:
<head>
<title></title>
<script type="text/javascript">
var uristr = window.location.search.substr(1);
var array = uristr.split('&&');
for (var i = 0; i < array.length; i++) {
var array1 = array[i].split('=');
alert(array1[0]);
}
--------------------//hash:返回#符號(hào)后的內(nèi)容
function showhash() {
alert(window.location.hash);
}
--------------------//host:服務(wù)器的名字
function showhost() {
alert(window.location.host);
}
--------------------//href:當(dāng)前載入的頁(yè)面的完整的URL
function showhref() {
alert(window.location.href);
}
--------------------//pathname:url中主機(jī)名后的部分
function showpathname() {
alert(window.location.pathname);
}
--------------------//protocal:URL中使用的協(xié)議
function showprotacal() {
alert(window.location.protocal);
}
--------------------//search:執(zhí)行g(shù)et請(qǐng)求的URL中問(wèn)號(hào)后面的部分,又稱(chēng)為查詢(xún)字符串
function showsearch() {
alert(window.location.search);
}
</script>
</head>
<body>
<input type="button" value="Hash" onclick="showhash();" />
<br />
<input type="button" value="host" onclick="showhost();" />
<br />
<input type="button" value="href" onclick="showhref();" />
<br />
<input type="button" value="pathname" onclick="showpathname();" />
<br />
<input type="button" value="protocal" onclick="showprotacal();" />
<br />
<input type="button" value="search" onclick="showsearch();" />
</body>
測(cè)試search的時(shí)候,需要從另一個(gè)頁(yè)面點(diǎn)擊一個(gè)連接,從瀏覽器地址欄穿過(guò)來(lái)值:
<body>
<a href="HTMLPage1.htm?name='王五'&&age=22">GO</a>
</body>
相關(guān)文章
JavaScript的設(shè)計(jì)模式經(jīng)典之建造者模式
建造者模式是設(shè)計(jì)模式的一種,將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。接下來(lái)通過(guò)本文給大家介紹JavaScript的設(shè)計(jì)模式經(jīng)典之建造者模式,感興趣的朋友一起學(xué)習(xí)吧2016-02-02js實(shí)現(xiàn)拉伸拖動(dòng)iframe的具體代碼
這篇文章介紹了js實(shí)現(xiàn)拉伸拖動(dòng)iframe的具體代碼,有需要的朋友可以參考一下2013-08-08Javascript從數(shù)組中隨機(jī)取出不同元素的兩種方法
這篇文章給大家分享了兩種Javascript從數(shù)組中隨機(jī)取出不同元素的方法,大家可以都學(xué)習(xí)學(xué)習(xí),這樣更能有助于大家的學(xué)習(xí)和理解,下面來(lái)一起看看吧2016-09-09純JavaScript代碼實(shí)現(xiàn)文本比較工具
之前項(xiàng)目需求需要寫(xiě)一個(gè)純js文本比較工具,在此小編把代碼分享在腳本之家平臺(tái)供大家參考2016-02-02JavaScript庫(kù)urlcat?之URL構(gòu)建器庫(kù)
這篇文章主要介紹了JavaScript庫(kù)urlcat之URL構(gòu)建器庫(kù),urlcat?是一個(gè)小型的JavaScript庫(kù),使構(gòu)建URL非常方便并防止常見(jiàn)錯(cuò)誤。下文來(lái)看對(duì)其詳細(xì)介紹吧,需要的小伙伴可以參考一下2022-02-02js實(shí)現(xiàn)為a標(biāo)簽添加事件的方法(使用閉包循環(huán))
這篇文章主要介紹了js實(shí)現(xiàn)為a標(biāo)簽添加事件的方法,基于閉包循環(huán)實(shí)現(xiàn)事件添加的功能,涉及javascript閉包與事件操作相關(guān)技巧,需要的朋友可以參考下2016-08-08Javascript遞歸打印Document層次關(guān)系實(shí)例分析
這篇文章主要介紹了Javascript遞歸打印Document層次關(guān)系的方法,實(shí)例分析了javascript中Document的層次關(guān)系,需要的朋友可以參考下2015-05-05