...

View Full Version : why window.location doesn't work in FF ?



marhoons
07-14-2010, 05:47 AM
dears,

am trying to redirect user from onchange event to another location in the page.
its work fine in IE but not in firefox :-(

why window.location doesn't work in FF ?

Html:



<form name="forder">
<select name="bycat" onchange="gocat()">
<option value=1>one</option>
<option value=2>two</option>
<option value=3>three</option>
</select>
</form>



javascript:



function gocat()
{
window.location ="index.php?show&cat=" + forder.bycat.options(forder.bycat.selectedIndex).value;
}



any idea????

Old Pedant
07-14-2010, 07:16 AM
Technically, it should be location.href (the "window." is optional).

Works for me all the time in all browsers.

But so does window.location, so far as I know.

But *probably* your actual problem is that you are using parentheses where square brackets are called for:


function gocat()
{
window.location ="index.php?show&cat=" + forder.bycat.options(forder.bycat.selectedIndex).value;
}

*ONLY* MSIE recognizes parentheses as indexers of the options.

Try:


function gocat()
{
location.href ="index.php?show&cat=" + forder.bycat.options[forder.bycat.selectedIndex].value;
}

*however*...

However all modern browsers also understand the ".value" of a <select> object, so long as it is not a <select multiple>. So you should be able to simply do:


function gocat()
{
location.href ="index.php?show&cat=" + forder.bycat.value;
}

Old Pedant
07-14-2010, 07:18 AM
And, finally, you could simplify this even further thus:


<form name="NOT NEEDED">
<select name="NOT NEEDED" onchange="location.href='index.php?show&cat='+this.value;">
<option value=1>one</option>
<option value=2>two</option>
<option value=3>three</option>
</select>
</form>

Philip M
07-14-2010, 07:18 AM
window.location = "index.php?show&cat=" + document.forder.bycat.options[document.forder.bycat.selectedIndex].value;

or simply

window.location = "index.php?show&cat=" + document.forder.bycat.value;


Old Pedant beat me to it! Why is he not in bed? :p FYI I am going on holiday today so you will not be troubled by me again for 10 days.


<select name="bycat" onchange="gocat()">
How does the user select option 1 onchange when One is the default?
There must be an initial "option" 'Please select' and the gocat() script must filter that out.


Expecting the world to treat you fairly because you are good is like expecting the bull not to charge because you are a vegetarian.

marhoons
07-14-2010, 10:54 PM
Old Pedant...
Thanks alot, worked with me.
i used (onchange="location.href='index.php?show&cat='+this.value).

Thanks again for great help.

Thanks Philip M too..

wish u all the best.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum