kiran.sangha
kiran.sangha

Reputation: 33

Opening drop down links in same tab

I'm using a dropdown menu to provide a few different links, but I want the links to open in a the same tab, not a new one. This is code I found, but I have a very lacking knowledge of Javascript

<script type="text/javascript">
 var urlmenu = document.getElementById( 'menu1' );
 urlmenu.onchange = function() {
    window.open(  this.options[ this.selectedIndex ].value );
 };
</script>

Upvotes: 0

Views: 1958

Answers (4)

atype
atype

Reputation: 41

Old post but I spent hours looking for a solution without anything working. This is simple and works for me.

*Although this works with no problem for all of these links on my website it throws a "refused to connect" error for some sites on the stack overflow preview - not sure why.

            <form id="pageFinderForm">

            <label class="input" for="PageFinder">Find a web page</label>

            <select id="page_list" name="page_list" form="pageform">
            <option value="https://stackoverflow.com">stack overflow</option>
            <option value="https://www.bing.com">Bing</option>
            <option value="https://www.google.com">Google</option>
            </select>

            <input type="button" name="Submit" value="Go!" onClick="window.open(page_list.value,'_self')">

            </form>

Upvotes: 0

Nano
Nano

Reputation: 1398

 var urlmenu = document.getElementById('menu1');
 urlmenu.onchange = function() {
    location.href = this.options[this.selectedIndex].value;
 };

You dont neet to open a new window if you just want to redirect the current tab. Use location.href insteat. The property-setter redirects your current Tab.

Upvotes: 0

x&#39;tian
x&#39;tian

Reputation: 734

Try this!

<html>
<body>
<form name="blah_blah">
<select name="ddmenu_name" id="ddmenu_name" style="width: 80% !important;">
<option value="" selected>Select Site</option>
<option value="http://www.yahoo.com">Yahoo!!!</option>
<option value="http://www.gmail.com">Gmail</option>
<option value="http://www.google.co.in">Google</option>
<option value="http://www.facebook.com">Facebook</option>
</select>
<input type="button" name="Submit" value="Go!"     onClick="window.open(ddmenu_name.value,'newtab'+ddmenu_name.value)">
</form>
</body>
</html>

Or might as well refer to this fiddle

http://jsfiddle.net/ChU8G/

Upvotes: 1

Ryan
Ryan

Reputation: 14649

So, you can open the content in an iframe element, but this is old school, and you should be using ajax.

var urlmenu = document.getElementById( 'menu1' );
var iframe = document.getElementById("my-page");
urlmenu.onchange = function() {
    iframe.src=this.options[ this.selectedIndex ].value ;
};

Upvotes: 0

Related Questions