2012-01-04 34 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>