location 对象,一个不太起眼的对象。
对于这个 URL “http://localhost:8000/lab/index.html?key=1#fly”,它包含了下面的属性。
hash: #fly host: localhost:8000 hostname: localhost href: http://localhost:8000/lab/index.html?key=1#fly pathname: /lab/index.html port: 8000 protocol: http: search: ?key=1
这篇文档里面有相关属性的说明。不过还有一点不太起眼的地方,hash 和 search 的先后顺序。
作为对比,有“http://localhost:8000/lab/index.html#fly?key=1”
hash: #fly?key=1 search:
各个浏览器上表现都是一样的,hash 先出现的话会吃掉 search 的,这让我有点费解,这个顺序还真那么重要吗?
当我们点击一个锚点的时候浏览器会在整个 URL 之后加上 hash的,这样似乎也挺好理解的。
只是如果需要手工拼接 URL,那就需要注意一下这个顺序了。