某些时候我们需要实现网页的自动跳转,比如404错误页可以加入代码让它自动跳转到首页,下面介绍三种html页面自动跳转的方法。
方法一:页面在头部加入meta标签 在头部加入 <meta http-equiv="refresh" content="时间/s;url=网址/http://www.dblog.cc">
代码如下:
<html>
<head>
<meta charset="UTF-8">
<title>html自动跳转</title>
<meta http-equiv="refresh" content="3;url=http://www.dblog.cc">
</head>
<body>
</body>
</html>
方法二:script实现自动跳转 第一种:比较常用:window.location.href = "http://baidu.com/";
代码如下:
<html>
<head>
<title>html自动跳转</title>
</head>
<body>
<script>
document.location = 'http://www.w3h5.com'
</script>
</body>
</html>
第二种:
self.location = "http://baidu.com/";
第三种:
top.location = "http://baidu.cn/";
第四种:只对IE系列浏览器有效,实用性不大。
window.navigate("http://baidu.cn/");
方法三:PHP实现
<?php
header("Location: http://baidu.cn/");
?>
方法四:动态显示的自动跳转代码(比较复杂,不推荐)
<html>
<head>
<meta charset="UTF-8">
<title>html自动跳转</title>
</head>
<body>
<form name=loading>
<p align=center>
<font color="#0066ff" size="2">正在跳转,请稍等</font><font color="#0066ff" size="2" face="Arial">...</font>
<input type=text name=chart size=46
style="font-family:Arial; font-weight:bolder; color:#0066ff; background-color:#fef4d9; padding:0px; border-style:none;">
<input type=text name=percent size=47
style="color:#0066ff; text-align:center; border-width:medium; border-style:none;">
<script>
var bar = 0
var line = "||"
var amount = "||"
count()
function count() {
bar = bar + 2
amount = amount + line
document.loading.chart.value = amount
document.loading.percent.value = bar + "%"
if (bar < 99) {
setTimeout("count()", 100);
}
else {
window.location = "http://www.dblog.cc";
}
}
</script>
</p>
</form>
<p align="center"> 如果您的浏览器不支持跳转,
<a style="text-decoration: none" href="http://www.dblog.cc">
<font color="#FF0000">请点这里</font>
</a>.
</p>
</body>
</html>