How can I make the Server automatically identify that the User is being used by a Mobile Device and send it to a mobile site
How can I make the Server automatically identify that the User is being used by a Mobile Device and send it to a mobile site
If it is Node.js with Express (so I noticed it looks like it's what you use) or it's on the front end you can then use
> First install via npm
:
npm install mobile-detect --save
Then in your project do something like this:
var MobileDetect = require('mobile-detect');
var md = new MobileDetect(req.headers['user-agent']);
if (md.mobile()) {
res.redirect('http://mobile.site.com');
} else if (String(req.get('host')).indexOf('mobile.') == 0) {
//Acaso acessar o site mobile no navegador Desktop ele irá redirecionar para o site normal
res.redirect('http://www.site.com');
}
If it is to be used in the browser, you can either download mobile-detect.min.js
or use CDN, like this:
<script src="//cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.1/mobile-detect.min.js"></script>
It should look like this:
<script src="mobile-detect.min.js"></script>
<script>
var md = new MobileDetect(window.navigator.userAgent);
if (md.mobile()) {
window.location.replace('http://mobile.site.com');
} else if (window.location.hostname.indexOf('mobile.') == 0) {
//Acaso acessar o site mobile no navegador Desktop ele irá redirecionar para o site normal
window.location.replace('http://www.site.com');
}
</script>