Version: Next

location对象

创建

window.location
location

方法

  • reload()——重新加载当前页面

属性

  • href()——设置或返回完整的URL
<body>
<input type="button" value="刷新" id="btn1"/>
<input type="button" value="去百度" id="btn2"/>
<script>
{
//reload方法,定义一个按钮, 点击按钮刷新当前页面
var button1 = document.getElementById('btn1');
button1.onclick = function () {
location.reload();
}
//获取href属性
var href = location.href;
document.write("当前路径:" + href + "<br/>");
var button2 = document.getElementById("btn2");
btn2.onclick = function () {
location.href = "https://www.baidu.com";
}
}
</script>
</body>

案例——自动跳转首页

<head>
<meta charset="UTF-8">
<title>自动跳转首页</title>
<style>
span {
color: red;
}
p {
text-align: center;
}
</style>
</head>
<body>
<p>
<span id="time">5</span>秒之后,自动跳转到首页...
</p>
<script>
var i = 5;
var time = document.getElementById("time");
function fun() {
i--;
time.innerText = i + "";
if (i <= 0) {
location.href = 'https://www.baidu.com';
}
}
setInterval(fun, 1000);
</script>
</body>