user4438160
user4438160

Reputation:

Javascript Mobile Redirect Not Working

Javascript

<script type="text/javascript">
    //deaktop = "m." + window.location.host + window.location.pathname;
    if ($(window).width() < 989) {
        document.location = "m." + window.location.host + window.location.pathname;
    }
</script>

Outcome URL

http://abcdfav4.com/Grown-Ups/KickStarterCampaign/Risks-and-Challenges/m.abcdfav4.com/Grown-Ups/KickStarterCampaign/Risks-and-Challenges/Hi-Tech-Perspective.html

So my script seems to be getting the current URL then adding "m." + window.location.host + window.location.pathname to the end whereas I just want to go to "m." + window.location.host + window.location.pathname

Upvotes: 0

Views: 34

Answers (2)

Reeno
Reeno

Reputation: 5705

You're missing the protocol:

document.location = window.location.protocol + '//m.' + window.location.host + window.location.pathname;

Upvotes: 0

Filipe Merker
Filipe Merker

Reputation: 2446

Your problem is with the 'http' protocol and, maybe, with the 'www' too.

Try this:

var url = window.location.protocol+"//m."+ window.location.href.replace('http://', '').replace('www.', '');

window.location.href = url;

Upvotes: 1

Related Questions