Skip to content

Instantly share code, notes, and snippets.

@yasuoza
Created February 20, 2012 08:25
Show Gist options
  • Save yasuoza/1868414 to your computer and use it in GitHub Desktop.
Save yasuoza/1868414 to your computer and use it in GitHub Desktop.
User Agent distinction
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;
}
@yasuoza
Copy link
Author

yasuoza commented Mar 2, 2012

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.

<script type="text/javascript" src="/path/to/ua.js"></script>

This code redirect *.com to *.com/i/.
It means
http://foobar.com/ will be http://foobar.com/i/, and
http://foobar.com/foo/entry.php will be http://foobar.com/i/foo/entry.php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment