Connecting Tech Pros Worldwide Help | Site Map

Javascript code only works in Netscape, yet no Scirpt error???

  #1  
Old July 23rd, 2005, 08:06 PM
John
Guest
 
Posts: n/a
Code is as follows:

document.write('<FORM name="horseform">');
document.write(' <SELECT SIZE="1" NAME="horsebrandslist" STYLE="font-size:
8pt">');
document.write(' <OPTION VALUE="http://my.domain.com/cat1.html">Category
1</OPTION>');
document.write(' <OPTION VALUE="http://my.domain.com/cat2.html">Category
2</OPTION>');
document.write(' <OPTION VALUE="http://my.domain.com/cat3.html">Category
3</OPTION>');
document.write(' </SELECT>');
document.write(' <a href="javascript:;" title="Click here"
onclick="goto_PetCategoryURL(document.horseform.el ements.horsebrandslist)"><img
border="0" src="../images/horse/someimage.gif" width="29" height="20"
alt="Click here" align="absbottom"></a>');
document.write('</FORM>');


In a separate .js file, the function "goto_PetCategoryURL" looks like this:

function goto_PetCategoryURL(object) {
if (object.selectedIndex > 0) {
window.location.href = object.options[object.selectedIndex].value;
}
return;
}


In Netscape (I use version 7), all works OK. In IE or Opera, when I click
the image, nothing happens. No error shows in the Opera JavaScript Console
or the bottom left of IE.

Who can solve this mystery?

Thanks,
Don


  #2  
Old July 23rd, 2005, 08:06 PM
Lee
Guest
 
Posts: n/a

re: Javascript code only works in Netscape, yet no Scirpt error???


John said:[color=blue]
>
>Code is as follows:
>
>document.write('<FORM name="horseform">');
>document.write(' <SELECT SIZE="1" NAME="horsebrandslist" STYLE="font-size:
>8pt">');
>document.write(' <OPTION VALUE="http://my.domain.com/cat1.html">Category
>1</OPTION>');
>document.write(' <OPTION VALUE="http://my.domain.com/cat2.html">Category
>2</OPTION>');
>document.write(' <OPTION VALUE="http://my.domain.com/cat3.html">Category
>3</OPTION>');
>document.write(' </SELECT>');
>document.write(' <a href="javascript:;" title="Click here"
>onclick="goto_PetCategoryURL(document.horseform.e lements.horsebrandslist)"><img
>border="0" src="../images/horse/someimage.gif" width="29" height="20"
>alt="Click here" align="absbottom"></a>');
>document.write('</FORM>');
>
>
>In a separate .js file, the function "goto_PetCategoryURL" looks like this:
>
>function goto_PetCategoryURL(object) {
> if (object.selectedIndex > 0) {
> window.location.href = object.options[object.selectedIndex].value;
> }
> return;
>}[/color]

1. You've got no way of going to cat1.html, since selectedIndex
for that choice is 0.

2. Your onclick handler should return false:
onclick="goto_PetCategoryURL(...);return false"

  #3  
Old July 23rd, 2005, 08:06 PM
mscir
Guest
 
Posts: n/a

re: Javascript code only works in Netscape, yet no Scirpt error???


John wrote:
[color=blue]
> Code is as follows:
> <snip>
>
> function goto_PetCategoryURL(object) {
> if (object.selectedIndex > 0) {
> window.location.href = object.options[object.selectedIndex].value;
> }
> return;
> }
>
> In Netscape (I use version 7), all works OK. In IE or Opera, when I click
> the image, nothing happens. No error shows in the Opera JavaScript Console
> or the bottom left of IE. Who can solve this mystery?
> Thanks,
> Don[/color]

The first selectedindex is 0, you ignore it with your if condition.

Also, I believe it's more efficient to concatenate a string several
times and to one document.write than to do multiple document.write.


This works in my IE 6, Firefox 1.0.1, Netscape 7.2:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>select</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function goto_PetCategoryURL(object) {
if (object.selectedIndex > -1){
alert(object.options[object.selectedIndex].value);
window.location.href = object.options[object.selectedIndex].value;
}
return;
}
</script>

</head>

<body>
<script type="text/javascript">
var s='<FORM name="horseform">';
s+=' <SELECT SIZE="1" NAME="horsebrandslist" STYLE="font-size:8pt">';
s+=' <OPTION VALUE="http://my.domain.com/cat1.html"
SELECTED>Category 1</OPTION>';
s+=' <OPTION VALUE="http://my.domain.com/cat2.html">Category
2</OPTION>';
s+=' <OPTION VALUE="http://my.domain.com/cat3.html">Category
3</OPTION>';
s+=' </SELECT>';
s+=' <a href="#;" title="Click Here"
onclick="goto_PetCategoryURL(document.horseform.el ements.horsebrandslist)">';
s+='<img border="0" src="someimage.gif" width="29" height="20"
alt="Click here" align="absbottom"></a>';
s+='</FORM>';
document.write(s);
</script>
</body>
</html>
Closed Thread