location对象的属性和方法应用(解析URL)

时间:2023-03-09 06:13:48
location对象的属性和方法应用(解析URL)
本文将与大家分享下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:返回#符号后的内容 
function showhash() { 
alert(window.location.hash); 

--------------------//host:服务器的名字 
function showhost() { 
alert(window.location.host); 

--------------------//href:当前载入的页面的完整的URL 
function showhref() { 
alert(window.location.href); 

--------------------//pathname:url中主机名后的部分 
function showpathname() { 
alert(window.location.pathname); 

--------------------//protocal:URL中使用的协议 
function showprotacal() { 
alert(window.location.protocal); 

--------------------//search:执行get请求的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>

相关文章