页面延迟跳转

1 要点javascript

1.1 html方法实现延迟跳转php

1.1.1 echo "<meta http-equiv=\"refresh\" content=\"5;url=".$url."\">\n";html

content属性里的“5”指明延迟5秒后跳转,中间用分号隔开,后面紧跟着的url指明要跳转到的页面连接,忽略则跳转到当前页面(至关于刷新)。java

 

1.2 JavaScript方法实现延迟跳转ide

1.2.1 echo "<script language=\"javascript\">setTimeout(\"window.location.href='".$url."'\",3000)</script>";函数

函数setTimeout(code, time)的功能是在延迟time个毫秒后执行code表明的代码,这里是在延迟3000毫秒也就是3秒后执行代码window.location.href=$url,跳转到$url表明的页面。ui

 

2 总结this

最开始我用header(“Location:somepage.php”);来进行跳转,可是这样一不能延迟跳转,二不能在调用header函数以前有任何输出,这样就不能在跳转前提示用户了。换成htmljs方法后的效果以下(二者代码不一样,但效果同样):url

 

 

 

3 源代码spa

说明:我将延迟跳转这个功能写成了一个函数,单独存在redirect.php中。第一个参数是跳转目标页面的URL,第二个参数是延迟时间内的通知消息。这样要使用这个函数时只须要include_once(“redirect.php”);就好了。

 

<?php

function redirect($url, $msg)

{

       //html方法实现页面延迟跳转

       /*echo "<html>\n";

       echo "<head>\n";

       echo "<meta http-equiv=\"refresh\" content=\"5;url=".$url."\">\n";

       echo "</head>\n";

       echo "<body>\n";

       echo $msg."</br>\n";

       echo "页面将在5秒后自动跳转...</br>\n";

       echo "<a href=\"".$url."\">若是没有跳转,请点这里跳转</a>\n";

       echo "</body>\n";

       echo "</html>\n";*/

      

       //js方法实现页面延迟跳转

       echo $msg."</br>\n";

       echo "页面将在3秒后自动跳转...</br>\n";

       echo "<a href=\"".$url."\">若是没有跳转,请点这里跳转</a>\n";

       echo "<script language=\"javascript\">setTimeout(\"window.location.href='".$url."'\",3000)</script>";

}

?>

相关文章
相关标签/搜索