如何重定向到移动版网站,但保持相同的页面?

时间:2014-02-06 19:16:21

标签: javascript redirect mobile

我正在尝试使用以下代码将用户重定向到我的网站的移动版本:

<script>
if ( (screen.width < 800) && (screen.height < 600) ) { 
    window.location = 'http://yoursite.com/m.asxp';
    } 
</script>

但问题是它总是重定向到主页。我希望重新定向到他们来自的同一页面。例如,如果他们转到yoursite.com/about,我希望它重新定向到yoursite.com/m.asxp/about。这可能吗?

1 个答案:

答案 0 :(得分:2)

只需替换它:

window.location = 'http://yoursite.com/m.asxp';

使用:

window.location = 'http://yoursite.com/m.asxp' + window.location.pathname;

<强> UPD 您还需要更改if-condition:

<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>
相关问题