web页面跳转的几种方式

时间:2023-03-09 00:22:56
web页面跳转的几种方式

可用客户端触发或服务端触发的方式来实现页面跳转。

客户端触发

方式一:使用Javascript

利用window.location对象的href属性、assign()方法或replace()方法来实现。

<script>
//使用href属性跳转
location.href ='https://www.sogou.com';
//或者,使用assign()方法跳转
location.assign('https://www.sogou.com');
//或者,使用replace()方法跳转
location.replace('https://www.sogou.com');
</script>

方式二:使用Html中<meta>标签来定义页面的元信息

<!-- 5秒钟后跳转到指定页面 -->
<meta http-equiv="refresh" content="5;url=http://www.baidu.com"/>

以上2种方式只是单纯的跳转,既不是重定向301也不是302。

服务端触发

使用PHP的header()函数

//使用302重定向跳转
header('Location:https://www.sogou.com'); //或者,使用301重定向跳转 (301和302有区别,注意使用)
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.sogou.com'); //注意:header()函数前不能有输出

(重定向301和302的区别见 http://www.cnblogs.com/shenxinpeter/p/5899204.html)

另外,还可以在Web服务器软件如apache中配置Rewrite来实现页面跳转。

最后,上面这些跳转对搜索引擎友好的只有301重定向。