Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Dreamweaver

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Overcoming Scripting Differences

When it comes to overcoming potential script differences, Dreamweaver really does all the hard work for you by providing a good foundation of behaviors with strong and cross-browser-compatible code. There will come a time, however, when you will need to write or copy some custom JavaScript.

In most cases, basic JavaScript will work happily in most major browsers, but there are obviously some things that won't. Whether there are two different versions of the code needed, or whether you require the code to be used only for one particular browser (for example, to be used in Internet Explorer but not Navigator), eventually you will need to detect which browser is being used to view the page. What's more, browsers change from version to version, so what works in IE 5 might not work in IE 5.5.

Unfortunately, Dreamweaver provides no simple solution for selecting script to execute depending on the browser. Based on the code that the Check Browser behavior uses, however, the logical solution is this:

<script language="JavaScript" type="Text/JavaScript">
<!--
var app=navigator.appName, verStr=navigator.appVersion, _version = parseFloat(verStr);
if (app.indexOf('Netscape') != -1) {
   alert("netscape " + version); // script for Navigator
   }
if (app.indexOf('Microsoft') != -1) {
   alert("ie " + version); // script for Internet Explorer
   }
//-->
</script>

This code example uses a simple alert() function to display the name and version of the browser being used. This is just to show that the script is detecting the browser correctly, and that the alert() functions should be replaced with the browser-specific code to be implemented.

Of course, if you have already gone to the trouble of detecting browsers and redirecting to browser-specific pages, the use of browser-dependent JavaScript is not a problem. Just be careful to thoroughly test any script in the target browsers.

Remember, as well, that not every user will access your site via the home page. Search engines will index different lots of pages from your site, and visitors may follow external links to the site; so you can't guarantee that every user will hit your browser-detection code unless it is included in every page.

This is an important factor, and you may want to address the problem by including the code in an external JavaScript file, which is then linked to every page.

An alternative way to deal with this sort of code is to check whether the browser supports the code you are trying to use before you use it—for instance, if you are attempting to perform an image rollover effect, but need to know whether the browser supports it. This code would address the issue:

if (document.images) {
   alert("This browser supports rollovers")
}

This code checks to see whether the browser can return document.images as true and, if so, executes the code. This is the property that a browser must support to perform a rollover; so if the property doesn't exist (causing the value to return as false), the code is skipped. This is sometimes a better way to address browser-compatibility problems when it comes to JavaScript.

  • + Share This
  • 🔖 Save To Your Account