简介
很多时候我们需要Web页具备有自动跳转功能,
例如,网站中的用户登录、发帖及回复或留言簿中的留言和回复等操作成功后,若用户没有任何鼠标点击操作,过了一定的时间,页面自动跳转到预设的页面。
本文讨论网页自动跳转的几种实现方法。
代码
HTML方式(可隐藏跳转后网址)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>轻刻年轮</title>
<frameset framespacing="0" border="0" rows="0" frameborder="0">
<frame name="main" src="https://www.ly522.com" scrolling="auto" noresize></frameset>
</head>
<body></body>
</html>
HTML
<meta http-equiv="refresh" content="0; url=https://www.ly522.com">
JavaScript
<script language="javascript">
top.location='https://www.ly522.com';
</script>
Apache(301)
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://www.ly522.com$1 [R=301,L]
ASP
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","https://www.ly522.com"
Response.End
%>
ASP.NET(301)
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", https://www.ly522.com);
}
</script>
Per
$q = new CGI;
print $q->redirect("https://www.ly522.com");
Pyton
from django import http
def view(request):
return http.HttpResponseRedirect('https://www.ly522.com')
PHP
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.ly522.com");
exit();
?>
跳转到本地页面-PHP
<?php
header("Location: /path/to/page.php");
exit;
?>
延时跳转
<?php
sleep(3);
header("Location:
exit;
?>
通过调用sleep函数延时3秒,然后再进行页面跳转。这种方式常用于一些提示页面或者倒计时页面的实现。
© 版权声明
THE END
暂无评论内容