Write a function that will successfully detect the name of the browser.
This says if it can find the string being passed to this function within the navigator.userAgent property, then return true. You can set browserTest to "MSIE" (Microsoft Internet Explorer), "Firefox," "Safari," "Opera" or "Chrome" when you call the function.
Use the function to run browser-specific code.
// insert code written for MSIE
This says if this browser is Microsoft Internet Explorer, then perform any scripts within this condition. As stated before, this could be done with "Firefox," "Safari," "Opera" or "Chrome" instead.
Consider other options. This may look simple enough, but it can become a big mess real quick if you want to determine browser version number, or worse, if a new browser comes along that this function was not designed to predict. The more graceful alternative is object detection.
Choose an object to detect. Only MSIE has a "document.all" object. Only Opera has a "window.opera" object. Older versions of MSIE do not support the "document.getElementsByClassName" object. However, with object detection, there is not as much need to remember all of that browser compatibility stuff. If you want to use an object that you think may not work in every browser, just make a conditional statement to see if the browser detects its existence before using it.
This says if the getElementsByClassName object exists, then execute the code within the following brackets.
Use the object that was tested for now that the browser is sure it exists, then close the conditional statement.
myElementArray = document.getElementsByClassName("myClass");
This says use getElementsByClassName to make an array "myElementArray" of all elements with their class set to "myClass."
Use an "else" statement after the "if" condition to include a coding alternative to the object being tested for. This is sometimes optional.
// code an alternative to the getElementsByClassName object
- Photo Credit Polka Dot RF/Polka Dot/Getty Images
How to Add a Title to a Web Page With Dreamweaver
Some search engines use titles to index Web pages. Titles are also used to identify pages in a browser's history and as...