1

I need to setup a onclick where if you click on the div with the id of "version" it calls the below script.

html

<div id="version">CONTINUE</div>

js

if (/Android\s+([\d\.]+)/.test(navigator.userAgent)){ 
 var anversion=new Number(RegExp.$1) 
 if (anversion=<3)
  window.location = 'main2.html'
 }
else
 window.location = 'main.html'
1
  • Useragent sniffing can be a bad practice.. just an FYI Commented Feb 7, 2012 at 20:55

3 Answers 3

3
$("#version").click(function() {
  // Your code here...
});

For more details see the jQuery API on .click()

EDIT: I also corrected your code. It then looks like this:

if(/Android\s+([\d\.]+)/.test(navigator.userAgent)) { 
  var anversion = parseInt(RegExp.$1);
  if(anversion <= 3) {
    window.location.href = 'main2.html';
  } else {
    window.location.href = 'main.html';
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but Im not even redirected to main.html on click
That is, because your code is buggy. Please use window.location.href = 'main.html'; and set appropiate semicolons.
The code only redirects on Android browsers. Are you running the code on your phone?
0

Have a look at the following: http://api.jquery.com/click/

Comments

0

Try this:

$("#version").click(function() {
    if (/Android\s+([\d\.]+)/.test(navigator.userAgent)) { 
        window.location.href = Number(RegExp.$1) <= 3 ? 'main2.html' : 'main.html';
    }
});

Your code has errors.

  • anversion=<3 should be anversion <= 3
  • new Number(RegExp.$1) should be Number(RegExp.$1) otherwise you create an object instead of converting string to number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.