JavaScript实现页面的自动跳转
2019-03-18 本文已影响0人
Leophen
(location和history对象的运用)
实现效果:
进入页面显示
1.gif
点击返回自动跳转至主页
附上代码:
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象-自动跳转</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
div{
text-align:center;
}
</style>
</head>
<body>
<div>
<div>
<h1>操作成功</h1>
</div>
<div>
<p><span id="second">5</span>秒后自动回到主页</p>
</div>
<div>
<a href="javascript:back();">返回</a>
</div>
</div>
<script type="text/javascript">
//获取显示秒数的元素,通过定时器来更改秒数
var num=document.getElementById("second").innerHTML;
function count(){
num--;
document.getElementById("second").innerHTML=num;
//通过window的location和history对象来控制网页的跳转
if(num==0){
window.location.assign("https://cn.bing.com/?mkt=zh-CN");
}
}
window.setInterval("count()",1000);
function back(){
window.history.back();
}
</script>
</body>
</html>