js实现页面跳转的三种方案

首页 / 计算机科学 / 正文
<html>
<head>
    <script language="javascript">
        <!--
        function old_page() {
            window.location = "http://www.baidu.com"
        }

        function replace() {
            window.location.replace("http://www.baidu.com")
        }

        function new_page() {
            window.open("http://www.baidu.com")
        }

        -->
    </script>
</head>
<body>
<input type="button" οnclick="new_page()" value="new_page"/>
<br/>
<input type="button" οnclick="old_page()" value="old_page"/>
<br/>
<input type="button" οnclick="replace()" value="replace"/>
</body>
</html>

关于window.locationwindow.open的区别:

  • window.location = "http://www.baidu.com" 跳转后有后退功能
  • window.location.replace("http://www.baidu.com") 跳转后没有后退功能
  • window.open("http://www.baidu.com") 要新的窗口打开链接
注:window.location.replace的应用场景:
从 Index1 页面跳转到 Index2 页面后,进行后退操作时,可以把来自 Index2 页面的返回挡在 Index2 页面,不返回 Index1 页面
评论区
头像
文章目录