Link to home
Start Free TrialLog in
Avatar of fgict
fgict

asked on

Javascript - Open in same Window

Hi,
Once the user selects a Town and clicks "Go" I wish the selected page to open in the same window and not load to "_blank" as it does at present. (see code below)


<SELECT id=discover style="FONT: 11px Arial; WIDTH: 120px; HEIGHT: 18px" name=select>
<OPTION value="" selected>Choose Region:</OPTION>
<OPTION value="../towns/town.asp?ID=1">Town1</OPTION>
<OPTION value="../towns/town.asp?ID=2">Town2</OPTION>
<OPTION value="../towns/town.asp?ID=3">Town3</OPTION>
<OPTION value="../towns/town.asp?ID=4">Town4</OPTION>
<OPTION value="../towns/town.asp?ID=5">Town5</OPTION>
</SELECT>

<SCRIPT type=text/javascript>
<!--
function jumptolink(what){
var selectedopt=what.options[what.selectedIndex]
var URL=selectedopt.value
window.open(URL)
}                                          
//-->
</SCRIPT>
<A style="FONT-WEIGHT: bold; LEFT: 0px; WIDTH: 18px" onclick="jumptolink(document.getElementById('discover'))" href="javascript:">GO</A>
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Hi,

replace window.open(URL); with window.location = URL; or document.location = URL;

-r-
Avatar of fgict
fgict

ASKER

Hi Roonaan

thanks for your response, unfortunately none of the suggested solutions worked :(
Can you try adding an alert like so:
alert(URL);

Is the url filled as expected?

-r-
Try this,

window.open('test.html','_self','height=200,width=400,status=yes,toolbar=no,menubar=no,location=no');

if u need more help refer this URL  -  http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/open_0.asp

Regards - G.
Avatar of fgict

ASKER

@ Roonaan

The Javascript alert shows:

../towns/town.asp?ID=4
Then window.location = URL; should work. Try window.location.href=URL; or window.location.replace(URL);

-r-
window.location.href=url should work
Avatar of fgict

ASKER

Sorry none of the examples you sent me worked when I tested it in Internet Explorer, yet it did work when I tried the line window.location=URL; in Firefox

Looks like a bug in IE 6 ?
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fgict

ASKER

Thanks Roonaan,

That did the trick, don't know why that version worked..but it did..many thanks again for all you help and patience!

fgict :)
return false on the onClick is what did it
and it is window.location or window.location.replace(url) that is the correct syntax
document.location is deprecated

Alternative would be


<SCRIPT type=text/javascript>
<!--
function jumptolink(what){
return what.options[what.selectedIndex].value
}                                  
//-->


<A style="FONT-WEIGHT: bold; LEFT: 0px; WIDTH: 18px" onclick="this.href=jumptolink(document.getElementById('discover'))" href="#">GO</A>
WITHOUT the return false