MIkey27
MIkey27

Reputation: 25

Javascript mobile detection

I am trying to make a javascript alert box that asks the user if he would like to go to the mobile page or not. Right now I have this code :

if (screen.width <= 699) {
   alert document.location = "/mobile";
}
else {
     alert ("Thanks!");
}

Upvotes: 1

Views: 3565

Answers (3)

gion_13
gion_13

Reputation: 41533

You could test the navigator.userAgent property for mobile keywords to determine if the user is navigating your site through a device, and then he would have to confirm weather to navigate using the mobile or the full version of the site :

var isMobile = /iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent);
if(isMobile && confirm('do you want to see the mobile site?'))
    // navigate to the mobile version of the site
    location = '/mobile';

Upvotes: 4

Qorbani
Qorbani

Reputation: 5905

I recommend using following website's code:

What it does it checking client's userAgent string to find out if it's mobile or not. It can detect most mobile devices also provide more ways to detect including jQuery, JavaScript, PHP, C# and ...

Hope this helps :-)

Upvotes: 5

JiminP
JiminP

Reputation: 2142

Something like

if(navigator.userAgent.match(/Mobile/i)) document.location.href="./mobile"

might work.

Upvotes: 0

Related Questions