URL的基础

1.URL概念

     URL(Uniform Resoure Locator)统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

2.URL语法格式

 

比如:

https://www.baidu.com/index.html?id=1&page=1#name1

用 javascript 获得其中的各个部分如下:

window.location.href:整个 URl 字符串,返回值:"https://www.baidu.com/index.html?id=1&page=1#name1"。

window.location.protocol:协议部分,返回值:"https:"。

window.location.host:主机部分。返回值:"www.baidu.com"。

window.location.port:端口部分。如果采用了协议默认的端口,则返回空字符。本例返回值:""。

window.location.pathname:路径部分。返回值:"/index.html"。

window.location.search:查询(参数)部分。返回值:"?id=1&page=1"。

window.location.hash:锚点(片段)。返回值:"#name1"。

 

原文地址:https://www.cnblogs.com/AdairHpn/p/13429474.html