jQuery redirect mobile under certain circumstances

1) if it's a mobile - Redirect.

2) if it has mobile=0 do NOT redirect.

3) if it's a PC do NOT redirect.

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;
}

你可能感兴趣的:(jquery,mobile)