zoukankan      html  css  js  c++  java
  • 判断浏览器及设备的打开方式,自动跳转app中

    如果安装了APP则自动条状app,如果没安装则自动跳转下载页面

    <head>

    放在head中加载

    <script>
    function redirect() {
    var appUri;
    var appStoreUrl;
    var eventId = getQueryString("eventId");
    switch (getDevice()) {
    case "iOS": appUri = "yomovie-thing://?eventId=" + eventId; appStoreUrl = "http://www.baidu.com/thing"; break;
    case "Android": appUri = "yomovie-thing://?eventId=" + eventId; appStoreUrl = "http://cn.bing.com"; break;
    //default: appUri = "#"; appStoreUrl = "";
    }
    if (appUri != undefined) {
    $('<iframe />')
    .attr('src', appUri)
    .attr('style', 'display:none;')
    .appendTo('body');
    setTimeout(function () {
    document.location.href = appStoreUrl
    }, 25);
    }
    }

    function getDevice() {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
    return 'iOS';
    }
    else if (userAgent.match(/Android/i)) {

    return 'Android';
    }
    else {
    return 'unknown';
    }
    }

    function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
    }

    var browser = {
    versions: function () {
    var u = navigator.userAgent, app = navigator.appVersion;
    return { //移动终端浏览器版本信息
    trident: u.indexOf('Trident') > -1, //IE内核
    presto: u.indexOf('Presto') > -1, //opera内核
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
    ios: !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
    iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
    iPad: u.indexOf('iPad') > -1, //是否iPad
    webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
    };
    }(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
    }
    </script>
    </head>

    <body>

    放在body中加载
    <script>
    (function () {
    if (browser.versions.mobile) {//判断是否是移动设备打开

    var ua = navigator.userAgent.toLowerCase();//获取判断用的对象

    if (ua.match(/WeiBo/i) == "weibo") {

    //在新浪微博客户端打开
    }
    else if (ua.match(/QQ/i) == "qq") {

    //在QQ微信打开
    }
    else if (ua.match(/MicroMessenger/i) == "micromessenger") {
    //在微信中打开
    }
    else {
    redirect();
    }
    }
    })()
    </script>

    </body>

  • 相关阅读:
    TensorFlow 基础 (04)
    面向对象编程思想的介绍
    B2B、B2C、C2C、O2O的概念
    为什么我们需要域
    如何在阿里云服务器上搭建wordpress个人网站
    Ghost手动备份、还原系统详细图文教程
    IE浏览器下载文件保存时提示:“你没有权限在此位置中保存文件”解决办法
    电脑经常自动重启的一些解决办法
    ERP系统到底能做什么?
    SQL实用技巧:如何分割字符串
  • 原文地址:https://www.cnblogs.com/wangjiaojiao/p/4583821.html
Copyright © 2011-2022 走看看