location对象属性 | 返回值 |
---|
location.href | 获取整个url |
location.host | 获取主机(域名) |
location.port | 返回端口号 |
location.pathname | 返回路径 |
location.search | 返回参数 |
location.hash | 返回片段 #后面内容 常用于链接 锚点 |
location对象方法 | 功能 |
---|
location.assign | 跟href一样,可以跳转页面 |
location.replace | 替换当前页面,因为不记录历史,所以不能后退页面 |
location.reload() | 重新加载页面,相当于刷新按钮或者f5,如果参数位true,强制刷新ctrlf5 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
<script>
var btn=document.querySelector('button');
var div=document.querySelector('div');
var time=5;
setInterval(function () {
div.innerHTML='您将在'+time+'s后跳转';
time--;
if(!time){
location.href='http://baidu.com';
}
},1000)
</script>
</body>
</html>