限制页面只能手机访问的三种方法
由于种种原因,可能是不希望别人轻易得到您建站的代码,或者是布置在电脑界面上布局并不美观,您可能需要使用禁止电脑端访问页面代码。快站建设为您建议以下三种解决方案的代码。
识别微信手机端口
/* 以下代码设定,禁止电脑浏览器查看本页面,只许手机或者微信访问,否则跳转到引导页面 */
var util = (function(){
var u = navigator.userAgent.toLowerCase();
return {
isIphone : function(){return (RegExp("iphone").test(u) || RegExp("ipod touch").test(u))},
isIpad : function(){return RegExp("ipad").test(u)},
isAndroid : function(){return (RegExp("android").test(u) || RegExp("android 2").test(u))},
isMB : function(){return (util.isIphone() || util.isIpad() || util.isAndroid())}
};
})();
window.util = util;
(function(){
if( !util.isMB() ){
var useragent = navigator.userAgent;
if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
window.location.replace("http://m.isohu.top/97/50/p423812526f51d5")
}
}
})();
agent识别机械端口
function checkMobile ()
{
var flag = false;
var agent = navigator.userAgent.toLowerCase();
var keywords = [ "android", "iphone", "ipod", "ipad", "windows phone", "mqqbrowser" ];
//排除 Windows 桌面系统
if (!(agent.indexOf("windows nt") > -1) || (agent.indexOf("windows nt") > -1 && agent.indexOf("compatible; msie 9.0;") > -1)) {
//排除苹果桌面系统
if (!(agent.indexOf("windows nt") > -1) && !agent.indexOf("macintosh") > -1 ) {
for (var item in keywords) {
if (agent.indexOf(item) > -1 ) {
flag = true;
break;
}
}
}
}
return flag;
}
if(checkMobile()==false)
{
window.location.replace("http://m.isohu.top/97/50/p423812526f51d5")
}
最简单的识别方案
var useragent = navigator.userAgent;
if (useragent.match(/Mobile/i) != 'Mobile') {
window.location.replace("http://m.isohu.top/97/50/p423812526f51d5");
}
以上三段代码均可实现电脑端打开后识别端口,如果不是手机端打开的话,会自动跳转到指定的页面,简单替换网址就可以实现用电脑端的进一步引导。
以上代码复制后,需要添加
代码复制在/script的符号闭合之间,
用页面的JS组件放置,即可使用!
亲,赶紧来试试看吧!