所以我正在使用这个代码重定向手机上的用户:
<script type="text/javascript">
if (screen.width <= 720) {
window.location = "m/index.php";
}
</script>但是,我想知道页面当前链接的代码,然后自动将其放入代码本身。所以它基本上是这样的:
<script type="text/javascript">
if (screen.width <= 720) {
window.location = "m/CurrentPageURL.php";
}
</script>任何帮助都将是伟大的:D
发布于 2015-07-22 19:02:00
试试这个。
if (location.pathname.substr(0, 3) !== "/m/" && screen.width <= 720) {
location.href = "/m" + location.pathname;
}它检查您是否已经在/m/子目录中,然后在适当的情况下重定向到那里。只要你有相关的移动版页面,这应该可以在你网站的任何页面上工作。
要做相反的事情,只需添加这个...
if (location.pathname.substr(0, 3) === "/m/" && screen.width > 720) {
location.href = location.pathname.substr(3);
}我想补充的是,有更好的方法来处理移动检测。例如,我的手机比我的桌面显示器分辨率更高,但只有5英寸的屏幕。实际上,它应该被重定向到移动站点(如果站点没有响应),但使用下面的代码就不会了。
只是一些需要牢记的事情。
https://stackoverflow.com/questions/31561102
复制相似问题