首页 > 技术文章 > 有关Location对象,及URL的解析

chenqiushi 2015-03-17 16:54 原文

Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档。

Document对象的location属性也引用到Location对象:

window.location === document.location // true

 

Document对象另外还有一个URL属性,是文档首次载入后保存该文档的URL的静态字符串。如果定位到文档的片段标识符(如#somewhere),Location对象会做相应的更新,而document.URL属性却不会改变。

 

提取URL中?后面的部分可以使用Location对象的search属性。同时使用decodeURIComponent可以对?后的字符进行解码。

var name = decodeURIComponent(window.location.search.substring(1))  ||  "";

 其他常用的属性还有:location.href  location.hash等

推荐阅读