JavaScript基础学习 location的常见用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button>点击</button>
<script>
var btn = document.querySelector('button');
btn.addEventListener('click', function() {
// 记录浏览历史,所以可以实现后退功能
// location.assign('http://www.itcast.cn');
// 不记录浏览历史,所以不可以实现后退功能
// location.replace('http://www.itcast.cn');
// 刷新页面
location.reload(true);
})
</script>
</body>
</html>
五秒之后自动跳转页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=devcie-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button>点击跳转</button>
<div></div>
<script>
var div = document.querySelector('div');
var timer = 5;
fn();
function fn() {
if (timer == 0) {
location.href = 'https://www.baidu.com/';
} else {
div.innerHTML = '倒计时' + timer + '秒跳转页面';
timer--;
}
}
setInterval(fn, 1000);
</script>
</body>
</html>