19 - Browser detection and Page redirection in Javascript

Browser detection

There are several browsers out in the market. Each user prefers a browser for a specific reason and according to tastes and the ones each of them find convenient. Some browsers are kept abridge with the advancing technologies while some of them would lack the support and compatibility for a feature or two.

If the developer wants to customize how a feature on a web page should be adapted for certain browsers then the browser detection could come in handy. The alternative can be used and applied for the incompatible browser so that the user experience is not hindered.

navigator.appName can help determine the client software (browser) which the user is using to access the web page. Some CSS features, DOM manipulation features are not available with some browsers. Some applications may not run on a particular browser or two.

if(navigator.appName == "Netscape” || navigator.appName == "Microsoft Internet Explorer")
  alert("This web page is best displayed on Mozilla Firefox or Google Chrome!");
  alert("Welcome to our website!!");

Page redirection / Browser redirection

A page’s URL helps the browser to request a page from the server. If the page has been moved to some other path or the website itself has been moved to another domain then page redirection can come in handy.

Take another situation. Suppose you own a website for a supermarket. You have filed the pages in folders according to the category but now want to do so by brand. Now you can use page redirect to the new set of folders.

Page redirects are very helpful in that if some user bookmarks a page and the page or domain has been moved then the user can be redirected to the new location of the page instead of losing a user.

The window.location takes the value of string of the URL to be which the page is to be redirected and displays it.

A word of caution: Remember the easiest way to link a page would be using hyperlinks and not by page redirects. Page redirects are supposed to be used only when there has been a change of domain or the file structure of the website.

    window.location = “www.google.com”

If this statement is used on the body load of a document then the page will be redirected when the page is reloaded.

One can use the setTimeout function to cause a delay in the redirection.

Like us on Facebook