js 中window.location获取url各项参数详解

1,window.location方法后还还可以带href,search等参数

以下是参数介绍:

location对象 含有当前URL的信息. 属性 href 整个URL字符串.
protocol 含有URL第一部分的字符串,如http:
host 包含有URL中主机名:端口号部分的字符串.如//www.cenpok.net/server/
hostname 包含URL中主机名的字符串.如http://www.cenpok.net ;

port 包含URL中可能存在的端口号字符串.
pathname URL中"/"以后的部分.如~list/index.htm
hash "#"号(CGI参数)之后的字符串.
search "?"号(CGI参数)之后的字符串.
 2,例子

(1),pathname,search,href,是比较常用的几个参数下面就举一个这三个参数的例子。

    假如页面地址是:http://localhost/test/test.htm?id=1

<html>
<head>
</head>
<body>
<script languge=javascript>
alert(window.location.pathname);   --返回   /test/test.htm
alert(window.location.search);        --返回   ?id=1
alert(window.location.href);             --返回   http://localhost/test/test.htm?id=1
</script>
</body>
</html>

 (2),下面就举一个比较完整的例子

地址:http://www.111cn.net :80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

var data=window.location.href ;

输出值:console.log(data)=http://www.111cn.net :80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

 

var data=,window.location.protocol

输出值:console.log(data)=http:

 

 

var data=,window.location.host

 

输出值:console.log(data)=www.111cn.net

 

 

 

var data=window.location.port ;

 

URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符

 

输出值:console.log(data)=""

 

 

var data=window.location.pathname ;

输出值:console.log(data)=/fisker/post/0703/window.location.html

 

var data=window.location.search;

输出值:console.log(data)=?ver=1.0&id=6

 

var data=window.location.hash

输出值:console.log(data)=#imhere

 

猜你喜欢

转载自13473996167.iteye.com/blog/2281233