Window Location 可用于获取当前页面地址(URL)并把浏览器重定向到新的网页。
-
location.href
返回当前页面的URL
document.getElementById("demo").innerHTML = "页面位置是 " + window.location.href;
-
location.hostname
返回(当前页面的)因特网主机的名称。
document.getElementById("demo").innerHTML = "页面主机名是 " + window.location.hostname;
-
location.pathname
返回当前的页面的路径名
document.getElementById("demo").innerHTML = "页面路径是 " + window.location.pathname;
-
location.protocol
返回页面的web协议,返回http或https
document.getElementById("demo").innerHTML = "页面协议是 " + window.location.protocol;
//返回结果:页面协议是 http:
-
location.port
返回(当前页面的)互联网主机端口的编号,通常是80或443
document.getElementById("demo").innerHTML = "端口号是: " + window.location.port;
-
location.assign
加载新文档
<html>
<head>
<script>
function newDoc() {
window.location.assign("https://www.baidu.com")
}
</script>
</head>
<body>
<input type="button" value="Load new document" onclick="newDoc()">
</body>
</html>
标签:Location,demo,innerHTML,JS,window,location,document,页面 From: https://www.cnblogs.com/xinbing/p/17004299.html