Cut down on device detections with this handy javascript tweak
Published November 24, 2011 by Richard Uren

Kudos to Sterling who wrote in a while ago with this handy tweak. Using this you can place javascript on each page of your website without chalking up extra detections plus it loads the script dynamically. Genius ! Thanks Sterling.
(function () {
'use strict';
var head, script, siteid, domain, internal, mobilesite;
siteid = YOUR-SITE-ID-GOES-HERE;
domain = new RegExp('DOMAIN.com', 'i');
mobilesite = 'http://m.DOMAIN.com';
head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
internal = document.referrer.search(domain);
script.type = 'text/javascript';
script.src = 'http://api.handsetdetection.com/sites/js/' + siteid + '.js';
if (internal === -1) {
head.appendChild(script);
}
script.onload = function () {
if (HandsetDetection.ismobile === true) {
document.location = mobilesite;
}
};
}());
Get the Gist of it.
Image Credit : http://www.flickr.com/photos/peternijenhuis
Related posts:
- Our Mobile Detection API – Now serving from a location near you
- Optimizing for Mobile Websites : The 3 Template Trick
- Handset Detection in 2 easy steps.
Posted In How To | 0 Comments