简体   繁体   English

Javascript移动重定向不起作用

[英]Javascript Mobile Redirect Not Working

Javascript Java脚本

<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 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 因此,我的脚本似乎正在获取当前URL,然后添加"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到最后,而我只想转到"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. 您的问题出在'http'协议,也许还有'www'。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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