通过js判断访问来自移动端还是pc端从而去控制事件的逻辑或者元素的显示

通过js判断访问来自手机端还是pc端

function browserRedirect() {

var accessTerminal = "";
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
    accessTerminal = "notpc";
    } else {
    accessTerminal = "pc";
    }
    return accessTerminal;

}

手机端限制长按弹出复制菜单(限制手机端浏览器复制功能)

$(function(){
var accessTerminal = browserRedirect();
if(accessTerminal!="pc"){
$('#endText').css({"-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none",
"-moz-user-select":"none","-ms-user-select":"none","user-select":"none"});
$('#endText').attr("unselectable","on");
$('#endText').attr("onselectstart","return false;");
/*document.body.onselectstart=document.body.oncontextmenu=function(){return false;};
if (typeof(document.onselectstart) != "undefined") {       
   // IE下禁止元素被选取       
   document.onselectstart = function (event){
       if(event.target.tagName!="INPUT"){
           return false;
       }
   }      
} else {
   // firefox下禁止元素被选取的变通办法       
   document.onmousedown = function (event){
       if(event.target.tagName!="INPUT"){
           return false;
       }
   }      
   document.onmouseup = function(event){
       if(event.target.tagName!="INPUT"){
           return false;
       }
   }       
}*/
}
});


通过js判断访问来自那款浏览器

function visitBrowser(){

var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1;
if (isOpera) {
return "Opera"
}; 
if (userAgent.indexOf("Firefox") > -1) {
       return "FF";
}
if (userAgent.indexOf("Chrome") > -1){
  return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
       return "Safari";
}
if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
       return "IE";
}; 
}

猜你喜欢

转载自blog.csdn.net/firewolf1758/article/details/52253303