【问题标题】:Redirecting to mobile version before loading html content在加载 html 内容之前重定向到移动版本
【发布时间】:2014-07-01 12:33:36
【问题描述】:

我创建了我的桌面网站的移动网站版本。 现在,在我的桌面网站上,我只在使用移动设备时屏幕宽度小于 680 像素时才寻求重定向到移动设备。

代码:

$(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";
             }
         } 
        )};

代码运行良好,但问题是当用户从移动设备进入页面时,它会在瞬间加载一些 html 内容,然后才重定向到移动页面。在加载任何页面内容之前,我如何确保他被重定向?我错过了什么吗?我应该创建一个负责重定向的第三个空白页面吗?

提前感谢您对此事的任何了解。

【问题讨论】:

  • 您需要服务器端逻辑来执行此操作。如果检测到移动用户您的服务器应该加载移动页面。如果您使用php,这很容易。
  • 这是因为 $(document).ready() 在 DOM 准备好之前不会触发(即所有 HTML 都准备就绪)。在此之前,您需要找到一种触发脚本的方法。也许把函数放在head
  • .NET 服务器端在加载任何客户端之前运行?
  • @Oranges,我不知道.net,但这里是如何使用phpcode.google.com/p/php-mobile-detect
  • 把它移到头上,完美运行。非常感谢@OllyHodgson

标签: javascript jquery html mobile


【解决方案1】:

您可以在 head 中使用您的脚本,但最好在服务器上检查它并从服务器重定向到移动页面。要检查服务器,请参阅:Request.Browser.IsMobileDevice = false for Android, why?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多