JS 手机访问PC站跳对应的手机域名和路径

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
<script>
if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
	if (window.location.href.indexOf("?mobile") < 0) {
		try {
			if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
				var path = window.location.pathname;
				var domain = window.location.host;
				// 替换成对应的手机域名
				window.location.href = "http://m.onestopweb.cn"+path+"?"+domain;
			}
		} catch (e) {}
	}
}
</script>
</head>
<body>
<script>
	document.write(window.location.pathname);//获取路径
	document.write('<br>');
	document.write(window.location.host);//获取域名
	document.write('<br>');
	document.write(document.domain);//获取域名
</script>
</body>
</html>

  

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2327029