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

本文介绍了一种通过JavaScript实现的网页适配方案,该方案能够检测用户是否使用移动设备访问,并将移动设备用户重定向到特定的移动版网站地址。此方法对于希望为移动用户提供更佳体验的网站开发者来说非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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>

  

效果图:

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值