js 跳转方法

JavaScript010

js 跳转方法,第1张

1、跳转链接 在当前窗口打开

window.location.href=" http://www.baidu.com "

等价于<a href="baidu.com" target="_self">go baidu</a>

2、跳转链接 在新窗口打开

window.open(" http://www.baidu.com ")

等价于 <a href="baidu.com" target="_blank">go baidu</a>

3、跳转链接 返回上一页

window.history.back(-1)

4、跳转链接

self.location.href="baidu.com"

self 指代当前窗口对象,属于window 最上层的对象。

location.href 指的是某window对象的url的地址

self.location.href 指当前窗口的url地址,

去掉self默认为当前窗口的url地址,一般用于防止外部的引用

top.location.href:为引用test.html页面url的父窗口对象的url

如果你的网页地址是:[ http://www.a.com ,]别人的是[ http://www.b.com] ,

他在他的页面用iframe等框架引用你的[ http://www.a.com] ,那么你可以用:

if``(top.location.href!=self.location.href){

location.href=``"http://www.a.com"``

}

来转向你的页面,top指代的是主体窗口,这里top.location.href返回[ http://www.b.com]

<script language="JavaScript">

    function mobile_device_detect(url) {

        var thisOS = navigator.platform

        var os = new Array("iPhone", "iPod", "iPad", "android", "Nokia", "SymbianOS", "Symbian", "Windows Phone", "Phone", "Linux armv71", "MAUI", "UNTRUSTED/1.0", "Windows CE", "BlackBerry", "IEMobile")

        for (var i = 0 i < os.length i++) {

            if (thisOS.match(os[i])) {

                window.location = url

            }

        }

        //因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认

        if (navigator.platform.indexOf('iPad') != -1) {

            window.location = url

        }

        //做这一部分是因为Android手机的内核也是Linux

        //但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断

        var check = navigator.appVersion

        if (check.match(/linux/i)) {

            //X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件

            if (check.match(/mobile/i) || check.match(/X11/i)) {

                window.location = url

            }

        }

        //类in_array函数

        Array.prototype.in_array = function(e) {

            for (i = 0 i < this.length i++) {

                if (this[i] == e) return true

            }

            return false

        }

    }

    mobile_device_detect("需要跳转的手机网址")

</script>

要实现从一个页面A跳到另一个页面B,js实现就在A的js代码加跳转代码

JS跳转大概有以下几种方式:

第一种:(跳转到b.html)

<script language="javascript" type="text/javascript">

window.location.href="b.html"

</script>

第二种:(返回上一页面)

<script language="javascript">

window.history.back(-1)

</script>

第三种:

<script language="javascript">

window.navigate("b.html")

</script>

第四种:

<script language="JavaScript">

self.location=’b.html’

</script>

第五种:

<script language="javascript">

top.location=’b.html’

</script>