2012-01-04 55 views
0

我開發了現有網站的移動版本。我在網站的桌面版本的主頁中添加了一些JavaScript來處理重定向。該代碼適用於移動設備,但在重定向之前出現主頁的副本。任何人都可以提供幫助,或者有人知道如何在頁面加載之前運行JavaScript?下面是JavaScript代碼的一個例子。無縫移動重定向腳本

此外,該網站的桌面主頁是HTML和PHP不是一個選項。我們也不想做永久移動重定向。我們希望用戶能夠訪問桌面版本,如果他們想要的話。

代碼:

<!doctype html> 
<head> 

<!--start JavaScript code--> 
<script> 
    if (screen.width < 500 || 
    navigator.userAgent.match(/Android/i) || 
    navigator.userAgent.match(/webOS/i) || 
    navigator.userAgent.match(/iPhone/i) || 
    navigator.userAgent.match(/iPod/i) || 
    navigator.userAgent.match(/BlackBerry/) || 
    navigator.appName("Chrome")) { 
     window.location.replace("TexasLandBankMobile/default.html"); 

</script> 
<!--end JavaScript code--> 

<title>Test</title> 
</head> 
<body> 
<div id="container"> 
    This is not a mobile device <br/> 
</div> 
</div> 
</body> 
</html> 

任何幫助將不勝感激。

回答

0

爲什麼不使用反應式設計,並且不要將移動用戶發送到您的首頁?

而除非你在服務器級別上重定向(Apache重寫等),你不能做你所要求的。

+0

感謝您的回覆。我們沒有時間實施反應式設計。有沒有一種方法可以在服務器級別實現,但是如果他們願意的話,可以讓移動用戶轉到桌面版本。 – 2012-01-04 21:22:36

+0

@RobertDavis是的,當你在你的服務器上做重定向時,讓它在url中查找一個查詢字符串,例如'?desktop = true'和一個'desktop = true' cookie。如果該標誌已設置,請設置'desktop = true' cookie並讓它們通過主站點。如果cookie已設置,請讓他們進入主站點。在您的主網站上有一個鏈接,指出「移動版」,它會刪除cookie並將其重定向到移動版本 – tkone 2012-01-04 21:50:18

0

最好的辦法是在用戶訪問您的頁面之前重定向用戶,這樣用戶就不必等待網站的臃腫桌面版本下載。

但是,對於解決方法,您可以嘗試隱藏這兩個版本的頁面,然後將該站點的桌面/移動版本設置爲可見。

例如

<script> 

function isMobileDevice() { 
    return screen.width < 500 || 
     navigator.userAgent.match(/Android/i) || 
     navigator.userAgent.match(/webOS/i) || 
     navigator.userAgent.match(/iPhone/i) || 
     navigator.userAgent.match(/iPod/i) || 
     navigator.userAgent.match(/BlackBerry/) || 
     navigator.appName("Chrome")); 
} 

if (isMobileDevice()) { 
    window.location.replace("TexasLandBankMobile/default.html"); 
} else { 
    // set container div to visible 
    document.getElementById('container').style.visibility = 'visible'; 
} 

</script>