Created
February 20, 2012 08:25
-
-
Save yasuoza/1868414 to your computer and use it in GitHub Desktop.
User Agent distinction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_mobile () { | |
var useragents = [ | |
'iPhone', // Apple iPhone | |
'iPod', // Apple iPod touch | |
'Android', // 1.5+ Android | |
'dream', // Pre 1.5 Android | |
'CUPCAKE', // 1.5+ Android | |
'blackberry9500', // Storm | |
'blackberry9530', // Storm | |
'blackberry9520', // Storm v2 | |
'blackberry9550', // Storm v2 | |
'blackberry9800', // Torch | |
'webOS', // Palm Pre Experimental | |
'incognito', // Other iPhone browser | |
'webmate' // Other iPhone browser | |
]; | |
var pattern = new RegExp(useragents.join('|'), 'i'); | |
return pattern.test(navigator.userAgent); | |
} | |
if (is_mobile()) { | |
var newurl = document.URL.replace(/\.com\//,".com/i/"); | |
// Write some code here. | |
// If you want to let mobile user redirect to mobile site, | |
// write following code. | |
// | |
// window.location.href = newurl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Redirect to mobile site.
If you want to let user who uses mobile browser redirect to mobile site,
write above content and save as
ua.js
.Then, write script tag on HTML head.
This code redirect
*.com
to*.com/i/
.It means
http://foobar.com/
will behttp://foobar.com/i/
, andhttp://foobar.com/foo/entry.php
will behttp://foobar.com/i/foo/entry.php
.