hi,欢迎访问本站!
当前位置: 首页学习笔记正文

jQuery获取当前页面的URL信息

用户投稿 学习笔记 2947阅读

在网站时不少客户要求导航条具有高亮功能,如果换到以前肯定是一个页面一个页面的去写代码,那样的话太麻烦,而且还不利于后期的维护和修改。不过现在有了jQuery了可以省去好多的事,再麻烦的事不就是一行JQ代码的事,如果解决不了,那就二行。

window.location

jQuery中的window.location可以获取当前网页的URL地址,也可以通过不同参数获取不同的地址部份

hash:设置或获取 href 属性中在井号“#”后面的分段。

host:设置或获取 location 或 URL 的 hostname 和 port 号码。

hostname:设置或获取 location 或 URL 的主机名称部分。

href:设置或获取整个 URL 为字符串。

pathname:设置或获取对象指定的文件名或路径。

port:设置或获取与 URL 关联的端口号码。

protocol:设置或获取 URL 的协议部分。

search :设置或获取 href 属性中跟在问号后面的部分。

window.location.pathname

设置或获取对象指定的文件名或路径。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.pathname);

输出:/topic/index

window.location.href

设置或获取整个 URL 为字符串。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.href);

输出:http://localhost:8086/topic/index?topicId=361

window.location.port

设置或获取与 URL 关联的端口号码。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.port);

输出:8086

window.location.protocol

设置或获取 URL 的协议部分。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.protocol);

输出:http:

window.location.host

设置或获取 location 或 URL 的 hostname 和 port 号码。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.host);

输出:http:localhost:8086

window.location.search

设置或获取 href 属性中跟在问号后面的部分。

例:

http://localhost:8086/topic/index?topicId=361alert(window.location.search);

输出:?topicId=361

通过,jQuery中window.location对网页URL地址的获取操作,我们可以很轻松的获取到我们需要的东西,并加以利于。其实导航高亮用这种方法比较不错,通过对网址地址与导航连接中的地址比较就可以实现

标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消