簡體   English   中英

Javascript移動重定向不起作用

[英]Javascript Mobile Redirect Not Working

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>

結果網址

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

因此,我的腳本似乎正在獲取當前URL,然后添加"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

您的問題出在'http'協議,也許還有'www'。

嘗試這個:

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