Finishing Touches for a Common Browser Menu - Making the code work with other browsers
(Page 3 of 5 )
The non-image and image code (the first two codes you have downloaded) work with Internet Explorer, Netscape and Mozilla Firefox without any problems. I have shown you above how you can change the code to work with Opera or Safari. The problem now is that if you change the code to work with some other browser, it may not work again with any or all of the three original browsers for which we wrote it.
To allow the code work with the three browsers, and then make the same code work with some other browsers of your choice, do the following: for each statement that you have to change, you should put the particular statement for the particular browser in an “if else-if else-if…” statement as follows:
if (Opera)
{
//statements for Opera are used here
}
else if (Safari)
{
//statements for Safari are used here
}
else if (some other Browser)
{
//statements for some other browser are used here
}
-
etc.
-
else
{
// the default original statements for Internet Explorer, Mozilla Firefox,
// and Netscape is used here
}
You do this for any of our statements that do not work with a particular browser. The "if" or "else-if" condition test is for the browser the user is using. I will show you how to do that now. You can also test for the version, but I will not show you how to test for the version in this series. The above statement changes as follows (with a nuance):
if (navigator.appName == "Microsoft Internet Explorer")
{
//statements for Microsoft Internet Explorer are used here
}
else if (navigator.appName == "Opera")
{
//statements for Opera are used here
}
else if (navigator.appName == "Netscape")
{
//statements for that Netscape are used here
}
else
{
// alert that the code may not work well with your browser.
//put the statements that works for Internet Explorer, Mozilla Firefox and
//Netscape here
}
JavaScript has an object called the navigator object. This object has a property called appName. This property with the navigator object returns the name of the user’s browser. Our statement here is not the same as the statement above. There is a nuance, which you will see as I will explain in the next section.
Next: The nuance explained >>
More HTML Articles
More By Chrysanthus Forcha