现在的位置: 首页 > 综合 > 正文

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

2012年10月18日 ⁄ 综合 ⁄ 共 1292字 ⁄ 字号 评论关闭

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>

抱歉!评论已关闭.