Forum Moderators: open

Message Too Old, No Replies

Opening new window from drop down selection!

here`s what I`m trying to do.....

         

dreamcatcher

6:41 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I`m trying to do is open a new window when someone selects from a drop down selection. No buttons, just automatic.

Here`s my code:


<select name="cat" onChange="MyWindow=window.open('this.options[this.selectedIndex].value', 'MyWindow','toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes, left=200,top=0,screenX=100,screenY=60,resizable=yes, width=600,height=400');">
<option selected value="0">&nbsp;</option>
<option value="view_stats.php?action=show&display=all">All</option>
<option value="view_stats.php?action=show&display=4">Movies</option>
<option value="view_stats.php?action=show&display=2">Music</option>
<option value="view_stats.php?action=show&display=5">People</option>
</select>

Now its working in the sense that a new window is opening, but the URL has this.options[this.selectedIndex].value in it, which is obviously no good. I want the values of the options to be the URL.

This is probably something simple, but I really don`t know javascript.

My SECOND question is, how do I have it so that if someone selects the option with a value of 0, nothing happens?

Many thanks.

:)

Birdman

7:00 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's getting you is the fact that you have your object surrounded in quotes, so it's treated literally. I also shortened it a bit. (This.value) should work fine.

<select name="cat" onChange="MyWindow=window.open(this.value, 'MyWindow',....

Birdman

dreamcatcher

7:30 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Birdman, that works great. So close too. Oh well, I gave it my best shot.

Just one other thing, is it possible for the first selected value to return nothing if this is selected after any of the other options have been?

Birdman

8:11 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Surely!

Best thing to do now is move it to a function in the head of the page.

<script type="text/javascript">
function go(url){
if (val == 0) return false;
MyWindow = window.open(url, ..rest of winopen...);
}
</script>

...in the body:

<select name="cat" onChange="go(this.value)">

dreamcatcher

8:58 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Works great. I just had to change if (val==0) to if(url==0) though. Got an error saying that val was undefined.

Thank you my friend! :)