1. 程式人生 > >通過js判斷訪問來自移動端還是pc端從而去控制事件的邏輯或者元素的顯示

通過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";
}; 
}