【问题标题】:how to redirect to mobile version of site but keep same page?如何重定向到网站的移动版本但保持相同的页面?
【发布时间】:2014-03-03 22:22:43
【问题描述】:
我正在尝试使用此代码将用户重定向到我的网站的移动版本:
<script>
if ( (screen.width < 800) && (screen.height < 600) ) {
window.location = 'http://yoursite.com/m.asxp';
}
</script>
但问题是它总是重定向到主页。我希望重定向转到他们来自的同一页面。例如,如果他们访问 yoursite.com/about,我希望它重新定向到 yoursite.com/m.asxp/about。这可能吗?
【问题讨论】:
标签:
javascript
redirect
mobile
【解决方案1】:
只需替换这个:
window.location = 'http://yoursite.com/m.asxp';
与:
window.location = 'http://yoursite.com/m.asxp' + window.location.pathname;
UPD
您还需要更改 if 条件:
<script>
if (window.location.indexOf('m.asxp') == -1 && (screen.width < 800) && (screen.height < 600) ) {
window.location = 'http://yoursite.com/m.asxp' + window.location.pathname;
}
</script>