用HTML/JS/PHP方式实现页面延时跳转

WEB开发中经常会遇到页面跳转或延时跳转的需求,掌握各种页面跳转方式非常必要。

以下是我总结有用HTML/JS/PHP三类方式实现跳转的方法,例子皆为三秒后跳转到index.php页面。

1,HTML方法:

HEAD中添加<meta>标签

<meta http-equiv=”refresh” content=”3;url=’index.php’” >

2,JS控制跳转方法

A.Location直接加链接方式

<script type="text/javascript">

  setTimeout("window.location=('index.php'",3000);

</script>

B.Location.href方式

<script type="text/javascript">

  setTimeout("window.location.href='index.php'",3000);

</script>

C.Location.assign方式

<script type="text/javascript">

  setTimeout("window.location.assign('index.php')",3000);

</script>

D.Location.replace方式(注意页面是被“替换”掉了,不会在浏览器的历史记录被查询到)

<script type="text/javascript">

  Widdow.location.replace(‘index.php’);

</script>

E.JS历史记录go(n)方式(n表示对历史记录相对当前页的前进步数,n为负数表示返回以前的页面)

<script type="text/javascript">

  window.history.go(n);

</script>

F.JS历史记录go(url)方式(注意url必须是历史记录内的,不然页面不会进行跳转)

<script type="text/javascript">

  window.history.go(‘index.php’);

</script>

G.JS window.open方式,通过打开一个新窗口,实现跳转。(其第二个属性为可选目标选项,值可以是frame id/_blank等,第三个选项为新弹出窗口的具体设置选项,包括height/width等)

<script type="text/javascript">

  setTimeout("window.open('index.php',target,args)",3000);

</script>

3PHP脚本控制跳转方式,通过改写HTTP头信息来进行跳转

A.header refresh方式:

Header(“refresh:3;url=’index.php’”);

B. header location 方式 :

sleep(3);

Header(“location:index.php”);

要注意这种方式会导致无法进入当前页面。即若当前在register.php页面链接到login.php页面时,login.php页面内用header location方式跳转,页面会从register.php页面直接等待三秒跳转到index.php,不会进入到login.php页面,这是因为header location会对页面进行重定向。

如有错误,欢迎指正,谢谢。

posted @ 2015-11-01 11:28  枕边书  阅读(12171)  评论(1编辑  收藏  举报