- 用来解析url地址的功能模块,将url字符和url对象互相解析
- 字符转对象:
url.parse(); //解析(字符解析成对象)
const urlStr = 'http://www.baidu.com:3000/qwe/zxc/index.html?username=admin&assword=12312#top'
const urlObj = url.parse(urlStr);
console.log(urlObj);
- 执行文件后为解析的对象:
![在这里插入图片描述](https://img-blog.csdnimg.cn/0f4e3bd72b3e45088cdb47094d7e1dd7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I2J546H5oCqMTk5ODg4,size_15,color_FFFFFF,t_70,g_se,x_16)
- 对象转字符:
url.format(); //反解析(对象解析成字符)
const url0 = {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com:3000',
port: '3000',
hostname: 'www.baidu.com',
hash: '#top',
search: '?username=admin&assword=12312',
query: 'username=admin&assword=12312',
pathname: '/qwe/zxc/index.html',
path: '/qwe/zxc/index.html?username=admin&assword=12312',
href: 'http://www.baidu.com:3000/qwe/zxc/index.html?username=admin&assword=12312#top'
}
const str = url.format(url0);
console.log(str);
- 执行文件后为解析的对象:
4 url的组成:
- 地址栏:
location
location.href:完整的url
location.protocol:通信协议
location.hostname:域名
location.port:端口
location.host:域名+端口
location.pathname:路径名
location.search:查询数据(向后端发送的数据)
location.hash:锚点连接(哈希值)
location.reload()
:刷新页面location.assign("")
:跳转到指定地址,空字符为刷新当前页面![在这里插入图片描述](https://img-blog.csdnimg.cn/596c01cffad346fb836953d2fea8305d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I2J546H5oCqMTk5ODg4,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)