JavaScript基础学习 location的常见用法
- 五秒之后自动跳转页面
Document
点击
var btn = document.querySelector('button');
btn.addEventListener('click', function() {
// 记录浏览历史,所以可以实现后退功能
// location.assign('http://www.itcast.cn');
// 不记录浏览历史,所以不可以实现后退功能
// location.replace('http://www.itcast.cn');
// 刷新页面
location.reload(true);
})
五秒之后自动跳转页面
Document
点击跳转
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);