December 5, 2000 - Browser-Independent Scripting
December 5, 2000 Browser-Independent Scripting Tips: December 2000
Yehuda Shiran, Ph.D.
|
if
statements. You want to make your script as generic as possible, and use if
clauses as late as possible. The following script uses this technique. The switchEntries()
function is a common event handler for all browsers. The content of switchEntries()
is an if
statement that executes a different statement for every browser:
<FORM NAME="election">
<SELECT NAME="presidents">
<OPTION NAME="leaving">Al Gore
<OPTION SELECTED>George W. Bush
</SELECT>
<INPUT TYPE="button" VALUE="Switch first option" onclick="switchEntries()">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
function switchEntries() {
if (document.all) {
document.election.presidents.item(0).text = 'Bill Clinton';
}
else if (document.layers) {
document.election.presidents[0].text = 'Bill Clinton';
}
else if (document.getElementById) {
document.election.leaving.text = 'Bill Clinton';
}
}
// -->
</SCRIPT>
Click the button below and see how the first option is changing on the fly: