Skip to content

Instantly share code, notes, and snippets.

@rohankhudedev
Last active March 16, 2017 17:50
Show Gist options
  • Save rohankhudedev/37c46b12fbda1e21dab18968e89cb7f2 to your computer and use it in GitHub Desktop.
Save rohankhudedev/37c46b12fbda1e21dab18968e89cb7f2 to your computer and use it in GitHub Desktop.
Redirect desktop site to mobile site using javascript.
/*If you have two different sites, one for mobile and another for desktop. When any user try to visit your desktop site
from mobile, then use below code on your desktop website in index.html or import this below code as external JS.*/
// Add below code in your head tag
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if( isMobile.any() )
{
//provide your mobile site
window.location.assign("http://www.yoursite.com/mobile/");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment