url链接中抽取参数

对于一串链接url,获取其中的url参数:示例url: http://www.caibaojian.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

1.window.location.href-------------------------整个url字符串(在浏览中就是完整的地址栏) 返回值: http://www.caibaojian.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

2.window.location.protocol--------------------url的协议部分 返回值:http

3.window.location.host------------------------ URL 的主机部分 本例返回值:www.caibaojian.com

  1. window.location.port-----------------------URL 的端口部分 如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符 本例返回值:""

  2. window.location.pathname(URL 的路径部分(就是文件地址)) 本例返回值:/fisker/post/0703/window.location.html

  3. window.location.search--------------------查询(参数)部分除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值 本例返回值:?ver=1.0&id=6

  4. window.location.hash------------------------锚点 本例返回值:#imhere

注意

URL:http://b.a.com:88/index.php?name=kang&how=#when=2011#first

search:"?name=kang&how=" 第一个"?"之后

hash:"#when=2011#first" 第一个"#"之后的内容

为什么 window.location.search 为空?

答:注意上面的search和hash的区别,如果URL中“?”之前有一个“#”比如:“http://localhost:63342/index.html#/version?type=35&id=5”那么使用window.location.search得到的就是空(“”)。因为“?type=35&id=5”串字符是属于“#/version?type=35&id=5”这个串字符的,也就是说查询字符串search只能在取到“?”后面和“#”之前的内容,如果“#”之前没有“?”search取值为空。

猜你喜欢

转载自www.cnblogs.com/chchchc/p/12162298.html