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

WEB开发中常常会遇到页面跳转或延时跳转的需求,掌握各类页面跳转方式很是必要。javascript

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

1,HTML方法:java

HEAD中添加<meta>标签浏览器

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

2,JS控制跳转方法ui

A.Location直接加连接方式url

<script type="text/javascript">

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

</script>

B.Location.href方式spa

<script type="text/javascript">

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

</script>

C.Location.assign方式code

<script type="text/javascript">

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

</script>

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

<script type="text/javascript">

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

</script>

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

<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会对页面进行重定向。

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

相关文章
相关标签/搜索