【问题标题】:jQuery redirect mobile under certain circumstances在某些情况下 jQuery 重定向移动
【发布时间】:2011-08-26 22:34:20
【问题描述】:

我正在尝试让一个 jQuery 脚本在某些情况下重定向到我的网站的移动优化版本。基本上,如果浏览器是移动设备,它将重定向到移动设备,除非在 URL 中它得到参数“mobile=0”。我有办法检测它是否是移动的,我有一个插件可以让你使用 jQuery 从 URL 获取参数(http://www.mathias-bank.de/2007/04/21/jquery-plugin- geturlparam-version-2/) 有效。

我想要的流程是,如果它是桌面,将移动变量设置为 0,而不是重定向。然后,如果它是移动的,则重定向,除非移动变量被 url 设置为 0。

现在,如果我在移动浏览器上,它不会重定向。我已经测试过我的重定向可以在移动设备上运行,但是在添加了 url 参数后,它就停止了。

我开始编码,但由于我对 javascript 有点陌生,我知道我犯了一些错误。到目前为止我的代码是:

 var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad|android|iemobile|ppc|smartphone|blackberry|webos|)/);
    var mobile = $.getURLParam("mobile");
    if (!agentID) {
        mobile = "0";
    }
    else {
        return false;
    }
    if ($.getURLParam("mobile")==0) {
          return false;
    }
else {
    if (agentID) {
        window.location.replace("http://remiwebo.vacau.com");
    }
    else {
        return false;
    }
}

提前致谢!

【问题讨论】:

  • 那么,到目前为止你得到的代码有什么问题?
  • 现在,如果我在移动浏览器上,它不会重定向。我已经测试过我的重定向可以在移动设备上运行,但是在添加了 url 参数后,它就停止了工作。编辑:意识到我忘了添加。谢谢!
  • 如果在添加 $.getURLParam("mobile") 后它停止工作,您是否测试过是否从该 jquery 方法中获取值? (一条警报消息就足够了)。但是看看你的逻辑,如果它不是移动设备,你将永远无法解析移动参数
  • 我从问题中链接到的 jQuery 插件中获取了 $.getURLParam("mobile") 。 (它确实有效,我对其进行了测试以确保从中获得变量)你对我如何正确解析它有什么建议吗?我希望移动参数始终为 1,除非 URL 显示为 0,或者我在桌面上。

标签: javascript jquery redirect mobile


【解决方案1】:

我认为你的逻辑可能比它需要的复杂一点。

我想要的流程是,如果它是桌面,请将移动变量设置为 0,而不是重定向。然后,如果它是移动的,则重定向,除非移动 变量由 url 设置为 0。

1) 如果是移动设备 - 重定向。

2) 如果它的 mobile=0 则不要重定向。

3) 如果是 PC,请不要重定向。

在您的计算机上测试移动版本怎么样? (移动 = 1)?

var deviceAgent = navigator.userAgent.toLowerCase();
document.write("Device Agent: " + deviceAgent);
var agentID = deviceAgent.match(/(iphone|ipod|ipad|android|iemobile|ppc|smartphone|blackberry|webos|)/);

document.write("</br>Agent ID: " + agentID);


//This doens't work well on JSfiddle, but it wwill work (non jquery way)
var mobile = getUrlVars()["mobile"];
document.write("<br />Mobile : " + mobile);

  //If we're a cell phone AND if there is no sign of the mobile parameter, or it is other than 0, redirect.
  if (agentID.length > 3 && (!mobile || mobile != "0")) {
      document.write("<br />GO GO GO TO MOBILE SITE");
      window.location = "mobile.verison.of.my.site.com"
  }
  else {
      document.write("<br />DO NOT GO TO MOBILE");
      return false;
  }
      // Handler for .ready() called.


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

更新:如果在 deviceAgent 字符串中找不到任何内容,agentID 将 = ", ", 2 个字符的字符串。因此,您可以检查它的长度,而不是测试它是否为空。

http://jsfiddle.net/3zv64/

【讨论】:

  • 该代码的唯一问题是它在 PC 和移动设备上都进行了重定向。需要在 PC 上,移动设备自动设置为 0。
  • 谢谢!您的代码的一个问题是(例如)在我的 ipod touch 上,我得到了“ipod,ipod”。因此,我将 agentID.length > 3 反转为 agentID.length
猜你喜欢
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2011-11-19
  • 2011-10-30
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多