2013-07-11 26 views
5

我用這下面的代碼,如果網站在移動設備上的Javascript document.location重定向到一個錯誤的URL

<script type="text/javascript"> 
<!-- 
if (screen.width <= 978) { 
document.location = "mobile.mysite.com"; 
} 
//--> 
</script> 

我測試了它在許多設備上訪問重定向。爲什麼網站只能重定向到www.mysite.com/mobile.mysite.com而不是mobile.mysite.com

+1

您是否嘗試過使用絕對URL路徑而不是相對路徑? – ConcurrentHashMap

回答

7

嘗試這個東西

<script type="text/javascript"> 
<!-- 
if (screen.width <= 978) { 
document.location.href = "http://mobile.mysite.com"; 
} 
//--> 
</script> 
0

嘗試使用window.location.href代替document.location :)

+0

仍然無法正常工作。 :( – Snippet

2

你需要指定'http://'或者在一開始只是'//',否則URL作爲相對而不是絕對的治療。

if (screen.width <= 978) { 
    document.location.href = "//mobile.mysite.com"; 
}