🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# location 对象   window.location 对象用于获得当前页面的地址URL,并把浏览器重定向到新的页面。 ## location对象的属性 * location.hostname 返回web主机的域名 * location.pathname 放回当前页面的路径和文件名 * location.port 放回web主机的端口 * location.protocol 返回所使用的web协议 [http|https] * location.href 属性返回当前页面的URL * location.assign() 方法加载新的文档 ~~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>window.location对象</title> </head> <body> <button id="btn">按钮</button> <script> function getLoc(){ console.log( window.location.hostname); //返回web主机域名 console.log( window.location.pathname); //返回web主机名 console.log( window.location.port); //放回web主机的端口 console.log( window.location.protocol); //返回所使用的web协议 console.log( window.location.href); //属性返回当前页面的URL // location.assign("https://www.baidu.com"); // 加载新的文档 } var oBtn = document.getElementById('btn'); oBtn.addEventListener("click",getLoc); </script> </body> </html> ~~~