• H5唤起APP一些坑



    $(function () {

    function _openAppUrl(appUrl){
    var ua = navigator.userAgent.toLocaleLowerCase(),
    openBrowser = null,
    deviceVersion = 0,
    matchVersion = null,
    openAppType = "",
    downLoadUrl = 'http://pages.book.qq.com/pages/qidian/qddown/qdreader.htm';

    //如果是在微信内部点击的时候
    if(ua.indexOf("micromessenger") != -1 ){
    _openAppUrl = function(){
    alert("DEMO,请在移动端的浏览器查看!");
    }
    }else{
    //在浏览器打开,判断是在移动端还是在PC端
    if(matchVersion = navigator.userAgent.match(/OSs*(d+)/)){
    //赋值,并且判断
    //IOS设备的浏览器
    deviceVersion = matchVersion[1] || 0;

    if(deviceVersion - 9 >= 0){
    openAppType = "newType";
    }
    }else if(matchVersion = navigator.userAgent.match(/Androids*(d+)/)){
    //Android的设备
    deviceVersion = matchVersion[1] || 0;

    if(deviceVersion - 5 >= 0){
    openAppType = "newType";
    }

    }else{
    //PC端的设备
    openAppType = "pc";
    }


    if(openAppType == "pc"){
    _openAppUrl = function(){
    alert("DEMO,请在移动端的浏览器查看!");
    }
    }else if(openAppType == "newType"){
    //使用新的方法,尝试打开APP
    //IOS>9,Android>5的版本
    _openAppUrl = function(url){
    var history = window.history,
    body = $("body").eq(0),
    ifr = $('<iframe class = "full-screen dn" style = "z-index:101;border:none;100%;height:100%;" src="'+downLoadUrl+'"></iframe>');

    body.append(ifr);
    $(window).on("popstate",function(e){
    var state = history.state;

    if(!state){
    ifr.addClass("dn");
    }
    });

    function _show(){
    history.pushState({}, "下载APP链接页", "");
    ifr.removeClass("dn");
    }

    _openAppUrl = function(url){
    location.href = url;
    _show();
    }

    _openAppUrl(url);

    }
    }else{
    //使用计算时差的方案打开APP
    var checkOpen = function (cb){
    var _clickTime = +(new Date()),
    _count = 0,
    intHandle = 0;

    //启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束
    intHandle = setInterval(function(){
    _count++;
    var elsTime = +(new Date()) - _clickTime;

    if (_count>=100 || elsTime > 3000 ) {
    clearInterval(intHandle);
    //计算结束,根据不同,做不同的跳转处理,0表示已经跳转APP成功了
    if ( elsTime > 3000 || document.hidden || document.webkitHidden) {
    cb(0);
    } else {
    cb(1);
    }

    }
    }, 20);
    }

    _openAppUrl = function(url){
    var ifr = document.createElement('iframe');

    ifr.src = url;
    ifr.style.display = 'none';

    checkOpen(function(opened){
    if(opened === 1){
    location.href = downLoadUrl;
    }
    });

    document.body.appendChild(ifr);

    setTimeout(function() {
    document.body.removeChild(ifr);
    }, 2000);
    }

    }

    }

    _openAppUrl(appUrl);

    }

    var appUrl = "QDReader://";

    $("#tryOpenApp").on("click",function(){
    _openAppUrl(appUrl);
    return false;
    });

    });

  • 相关阅读:
    FiddlerScript修改特定请求参数下的返回值
    nginx设置反向代理后,页面上的js css文件无法加载
    通过外网访问内网服务器
    linux下使用正确的用户名密码,本地无法连接mysql
    合并重叠时间段C#
    数据库一直显示为单用户,解决办法
    windows下使用tomcat部署网站
    数据库一直还原中,解决办法
    通过mdf ldf文件还原数据库
    知道css有个content属性吗?有什么作用?有什么应用?
  • 原文地址:https://www.cnblogs.com/ThomasEdison/p/6256175.html
Copyright © 2020-2023  润新知