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