user3254973
user3254973

Reputation: 95

how to redirect to mobile version of site but keep same page?

I'm trying to re-direct users to the mobile version of my site using this code:

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

but the issue is it always re-directs to the homepage. i want the re-direct to go to the same page they came from. for example if they go to yoursite.com/about i want it to re-direct to yoursite.com/m.asxp/about. is this possible?

Upvotes: 2

Views: 87

Answers (1)

Danny Chernyavsky
Danny Chernyavsky

Reputation: 479

Just replace this:

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

with:

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

UPD Also you need to change 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>

Upvotes: 2

Related Questions