Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6364237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:08:18+00:00 2026-05-25T00:08:18+00:00

I am trying to get a jQuery script to redirect to a mobile optimized

  • 0

I am trying to get a jQuery script to redirect to a mobile optimized version of my site under certain circumstances. Basically, if the browser is mobile, it will redirect to the mobile, unless in the URL it gets the argument “mobile=0”. I’ve got a way to detect if it’s mobile, and I have a plugin which lets you get arguments from the URL with jQuery (http://www.mathias-bank.de/2007/04/21/jquery-plugin-geturlparam-version-2/) which works.

The flow I’d like to have is if it’s desktop, set a mobile variable to 0, and not redirect. Then, if it’s mobile, redirect unless the mobile variable is set to 0 by the url.

Right now, it isn’t redirecting if I am on a mobile browser. I have tested that my redirect works on mobile, but after adding the url parameters, it stopped working.

I started coding, but as I am a bit new at javascript, I know I am making some mistakes. My code so far is:

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

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:08:19+00:00Added an answer on May 25, 2026 at 12:08 am

    I think you’re logic might be a bit more complex than it needs to be.

    The flow I’d like to have is if it’s desktop, set a mobile variable to
    0, and not redirect. Then, if it’s mobile, redirect unless the mobile
    variable is set to 0 by the url.

    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.

    What about testing a mobile version on your computer? (mobile = 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;
    }
    

    update: agentID will = “, “, a 2 character string if it doesn’t find anything in the deviceAgent string. So rather than testing if it’s null, you can check the length of it.

    http://jsfiddle.net/3zv64/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pulling my hair out trying to get a jQuery script working that will
I am trying to find out how to get a jQuery script to work
I am trying to get jquery validate to work on multiple fields. Reason being
I am trying to get jquery to pick up variable that I am defining
I am trying to get simple jQuery to execute on my Content page with
I'm trying to get the jQuery slider to have set values to slide to
I'm trying to get an jquery ajax callback function to update the background colour
I'm in the process of trying to get the jQuery plugin, Uploadify , to
Trying to get comfortable with jQuery and I have encountered some sample code that
I am trying to get this awesome JQuery plugin working with SQL Server. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.