简体   繁体   中英

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 "m." + window.location.host + window.location.pathname to the end whereas I just want to go to "m." + window.location.host + window.location.pathname "m." + window.location.host + window.location.pathname

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;

您缺少协议

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM