判断手机终端并自动跳转js代码及使用实例

//Source Code

// JavaScript Document

function mobiledirect(f) {
    try {
        if (document.getElementById("bdmark") != null) {
            return
        }
        var b = false;
        if (arguments[1]) {
            var e = window.location.host;
            var a = window.location.href;
            if (isSubdomain(arguments[1], e) == 1) {
                f = f + "/#m/" + a;
                b = true
            } else {
                if (isSubdomain(arguments[1], e) == 2) {
                    f = f + "/#m/" + a;
                    b = true
                } else {
                    f = a;
                    b = false
                }
            }
        } else {
            b = true
        }
        if (b) {
            var c = window.location.hash;
            if (!c.match("fromapp")) {
                if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {                    
                    /*var curUrl = window.location.href;
                    var strPath = window.location.pathname;
                    //curUrl = curUrl.toLowerCase();
                    re = /^\/(wholesale|producttags)\//i;
                    if(strPath=="/" || re.test(strPath)){
                        curUrl = curUrl.replace("www.","m.");
                        location.replace(curUrl);
                        return;
                    }*/
                    if(f){
                        location.replace(f);
                        return;
                    }
                }
            }
        }
    } catch (d) {}
}
function isSubdomain(c, d) {
    this.getdomain = function(f) {
        var e = f.indexOf("://");
        if (e > 0) {
            var h = f.substr(e + 3)
        } else {
            var h = f
        }
        var g = /^www\./;
        if (g.test(h)) {
            h = h.substr(4)
        }
        return h
    };
    if (c == d) {
        return 1
    } else {
        var c = this.getdomain(c);
        var b = this.getdomain(d);
        if (c == b) {
            return 1
        } else {
            c = c.replace(".", "\\.");
            var a = new RegExp("\\." + c + "$");
            if (b.match(a)) {
                return 2
            } else {
                return 0
            }
        }
    }
};



//------------以下为压缩过---------------------

function uaredirect(f) {

    try {
        if (document.getElementById("bdmark") != null) {
            return
        }
        var b = false;
        if (arguments[1]) {
            var e = window.location.host;
            var a = window.location.href;
            if (isSubdomain(arguments[1], e) == 1) {
                f = f + "/#m/" + a;
                b = true
            } else {
                if (isSubdomain(arguments[1], e) == 2) {
                    f = f + "/#m/" + a;
                    b = true
                } else {
                    f = a;
                    b = false
                }
            }
        } else {
            b = true
        }
        if (b) {
            var c = window.location.hash;
            if (!c.match("fromapp")) {
                if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) {

                    location.replace(f)

                   /*

                    //自定义转向指定页面

                    f = window.location.href;
                    g = window.location.pathname;

                    re = /^\/(wholesale|producttags)\//i;

                    if ("/" == g || re.test(g))

                         return f = f.replace("www.", "m.");

                      */


                }
            }
        }
    } catch(d) {}
}
function isSubdomain(c, d) {
    this.getdomain = function(f) {
        var e = f.indexOf("://");
        if (e > 0) {
            var h = f.substr(e + 3)
        } else {
            var h = f
        }
        var g = /^www\./;
        if (g.test(h)) {
            h = h.substr(4)
        }
        return h
    };
    if (c == d) {
        return 1
    } else {
        var c = this.getdomain(c);
        var b = this.getdomain(d);
        if (c == b) {
            return 1
        } else {
            c = c.replace(".", "\\.");
            var a = new RegExp("\\." + c + "$");
            if (b.match(a)) {
                return 2
            } else {
                return 0
            }
        }
    }

};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现路由的跳转,首先需要在 Vue 项目中安装并引入 Vue Router,具体步骤如下: 1. 在终端中输入以下命令安装 Vue Router: ``` npm install vue-router --save ``` 2. 在 main.js 中引入 Vue Router 并挂载到 Vue 实例上: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' import Home from './components/Home.vue' import About from './components/About.vue' Vue.use(VueRouter) const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = new VueRouter({ routes }) new Vue({ router, render: h => h(App), }).$mount('#app') ``` 在上面的代码中,我们先引入 Vue Router 并通过 Vue.use() 方法注册路由器。接着,我们定义了两个组件 Home 和 About,然后创建了一个 routes 数组,其中包含了两个路由对象,每个路由对象都指定了一个路径和对应的组件。最后,我们通过 new VueRouter() 方法创建了一个路由器实例,并把 routes 作为参数传入。最后,我们把路由器实例挂载到 Vue 实例上。 3. 在组件中使用路由跳转 在组件中使用路由跳转分为两种情况:声明式和编程式。 (1) 声明式跳转 在模板中使用 `<router-link>` 组件可以实现声明式跳转,例如: ```html <template> <div> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> </div> </template> ``` 在上面的代码中,我们通过 `<router-link>` 组件指定了两个路由链接,分别对应着 Home 和 About 组件。当用户点击这两个链接时,路由器会自动跳转到对应的组件。 (2) 编程式跳转 在 JavaScript 中使用 `$router` 对象可以实现编程式跳转,例如: ```javascript export default { methods: { goToHome() { this.$router.push('/') }, goToAbout() { this.$router.push('/about') } } } ``` 在上面的代码中,我们分别定义了两个方法,分别用于跳转到 Home 和 About 组件。在方法中,我们通过 `$router.push()` 方法指定了跳转的路径,路由器会自动跳转到对应的组件。 以上就是在 Vue2 中实现路由跳转的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值