Javascript 加载html内容前重定向到移动版本

Javascript 加载html内容前重定向到移动版本,javascript,jquery,html,mobile,Javascript,Jquery,Html,Mobile,我已经创建了我的桌面网站的移动网站版本。 现在,在我的桌面网站上,只有在使用移动设备时屏幕宽度小于680px时,我才试图重定向到移动设备 代码: $(文档).ready(函数(){ if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){ var deviceWidth=window.orientation==0?window.scre

我已经创建了我的桌面网站的移动网站版本。 现在,在我的桌面网站上,只有在使用移动设备时屏幕宽度小于680px时,我才试图重定向到移动设备

代码:

$(文档).ready(函数(){
if(/Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test(navigator.userAgent)){
var deviceWidth=window.orientation==0?window.screen.width:window.screen.height;
if(navigator.userAgent.indexOf('Android')>=0&&window.devicePixelRatio){
deviceWidth=deviceWidth/window.devicePixelRatio;
}
如果(设备宽度<680){
window.location.href=“移动页面URL”;
}
} 
)};
代码工作得很好,但问题是当用户从移动设备进入页面时,它会在瞬间加载一些html内容,然后才重定向到移动页面。在加载任何页面内容之前,我如何确保他被重定向?我遗漏了什么吗?我是否应该创建一个负责重定向的第三个空白页


提前感谢您对此事的解释。

您可以在头脑中使用脚本,但最好在服务器上检查脚本并从服务器重定向到移动页面。要在服务器上检查,请参阅:

您需要服务器端逻辑来执行此操作。如果检测到移动用户,服务器应加载移动页面。如果您使用的是
php
,这很容易。这是因为
$(document).ready()
在DOM准备就绪(即所有HTML文件都准备就绪)之前不会启动。在此之前,您需要找到启动脚本的方法。也许把函数放在
头部
?.NET服务器端在加载任何客户端之前运行?@Oranges,我不知道
.NET
,但下面是如何使用
php
:把它放在头部,效果很好。非常感谢@OllyHodgson
$(document).ready(function () {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {

 var deviceWidth = window.orientation == 0 ? window.screen.width : window.screen.height;
 if (navigator.userAgent.indexOf('Android') >= 0 && window.devicePixelRatio) {
   deviceWidth = deviceWidth / window.devicePixelRatio;
 }
             if (deviceWidth < 680) {
                 window.location.href = "Mobile page URL";
             }
         } 
        )};