• 七角七分博客欢迎大家光临

电脑端网站在手机访问时,指定转向另一个页面的JS代码

常用技巧 七七 5年前 (2018-11-22) 0个评论

在有些时候,我们需要在手机访问网站时,把网站指向另一个网站或另一个目录,这时候,我们需要在默认首页上加一代JS,就可实在。如下代码,网址改成需要指向的网站或目录,保存成JS文件,放在网站的首页头部即可。

var domain = ["http://mh.benen.cn/forum.php"];
var al = new Array("iPad", "iPhone", "Android", "X11", "MIDP",                    
		"Opera Mobi", "Opera Mini", "BlackBerry", "HP iPAQ", "IEMobile",
		"MSIEMobile", "Windows Phone", "HTC", "LG", "MOT", "Nokia",
		"Symbian", "Fennec", "Maemo", "Tear", "Midori", "armv",
		"Windows CE", "WindowsCE", "Smartphone", "240x320", "176x220",
		"320x320", "160x160", "webOS", "Palm", "Sagem", "Samsung", "SGH",
		"Siemens", "SonyEricsson", "MMP", "UCWEB");
function toUrl() {
	var i = parseInt(Math.random() * (domain.length));
	window.location.href = domain[i];
}
function checkUA() {
	var userAgent = navigator.userAgent;
	var ifWap = "";
	for ( var mobile in al) {
		var m = al[mobile];
		var ms = new RegExp(m.toLowerCase());
		if (ms.test(userAgent.toLowerCase())) {
			switch (m.toLowerCase()) {
			case "ipad":
				ifWap = "ipad";
				toUrl();
				break;
			case "iphone":
				ifWap = "iphone";
				toUrl();
				break;
			default:
				ifWap = "other";
			    toUrl();
				break;
			}
		} else {
			ifWap = "PC";
		}
		if (ifWap != "" && ifWap != "PC")
			break;
	}
}
checkUA();
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址