I have coded few lines of java script code. But I am not sure that it is correct or not.
Few months back I published a desktop version website. but when a user coming through a mobile (like android, iPhone or may be windows phone), I want to redirect a new path. Here is the code:
// JavaScript Document
var uAgent = navigator.userAgent.toLowerCase();
if(uAgent.indexOf("android") > -1 || uAgent.indexOf("iphone") > -1 || uAgent.indexOf("windows phone") > -1) {
window.location = "http://website path will come here.";
}
Is it the correct code?
That will work, but its not a great way to do this for a couple of reasons:
Users with JavaScript disabled will not get redirected
Mobile users (some with slow browsers and tight bandwidth limits) will download your desktop page before being redirected to the mobile site.
A better approach if possible, would be to manage this via the web server configuration ideally without any redirects at all. Alternatively, consider serving the same content to all users and changing the layout with media queries.