美文网首页
js判断浏览器为pc还是移动端

js判断浏览器为pc还是移动端

作者: 临天下 | 来源:发表于2016-09-07 11:05 被阅读1095次
    
    <!-- saved from url=(0099)file:///C:/Users/Administrator/Documents/Tencent%20Files/1191102903/FileRecv/tanx-banner-pm-cm.html -->
    <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="user-scalable=no, width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    </head>
    <body onload="loadAndDisplayAd()">
    </body>
    <script>
    function loadAndDisplayAd() {
        var adimg = new Image();
        adimg.style.cursor="pointer";
        if (isPC()!=true) {//移动端将图片拉伸占满整个屏幕
            adimg.style.width=document.documentElement?document.documentElement.clientWidth:document.body.clientWidth;
            adimg.style.height=document.documentElement?document.documentElement.clientHeight:document.body.clientHeight;
        };
        adimg.onload = function() {
            //添加广告到页面中 
            //展示广告
            document.body.appendChild(adimg);
            showCallback();//曝光反馈
        }
        //广告图片地址
        adimg.src = 'http://gma.alicdn.com/imgextra/i3/10000025201164012/T29NeTXnlXXXXXXXXX_!!0-0-newland.jpg_360x360q90.jpg';
    
        //添加点击事件
        if(window.addEventListener){ // Mozilla, Netscape, Firefox 
            adimg.addEventListener('click', clickCallback, false); 
        } else { // IE 
            adimg.attachEvent('onclick', clickCallback); 
        } 
    }
    var showCallback=function(){
        var bimg = new Image();
        //反馈的地址
        bimg.src = 'http://img11.360buyimg.com/da/jfs/t181/152/1710372957/137/fc862484/53ba3868Nea2f6c42.png';
        window[new Date()] = bimg;
    }
    var clickCallback=function(){
    
        window.open("https://www.baidu.com");   
    
        var bimg = new Image();
        //反馈的地址
        bimg.src = 'http://cms-bucket.nosdn.127.net/catchpic/d/d5/d5dce887d5e1ef4b4a3f1ebb149a93c0.jpg?imageView&amp;thumbnail=550x0';
        window[new Date()] = bimg;
    }
    function isPC()  
    {  
       var userAgentInfo = navigator.userAgent;  
       var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");  
       var flag = true;  
       for (var v = 0; v < Agents.length; v++) {  
           if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }  
       }  
       return flag;  
    }            
    </script>
    </html>
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:js判断浏览器为pc还是移动端

          本文链接:https://www.haomeiwen.com/subject/prsjettx.html