在同一选项卡中打开下拉链接

时间:2022-12-09 20:21:00

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

我正在使用下拉菜单提供一些不同的链接,但我希望链接在同一个标​​签中打开,而不是新标签。这是我发现的代码,但我对Javascript非常缺乏了解

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

3 个解决方案

#1


1  

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/

#2


0  

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

因此,您可以在iframe元素中打开内容,但这是旧学校,您应该使用ajax。

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

#3


0  

 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.

如果您只想重定向当前选项卡,则不要打开新窗口。使用location.href insteat。属性设置器重定向您当前的Tab。

#1


1  

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/

#2


0  

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

因此,您可以在iframe元素中打开内容,但这是旧学校,您应该使用ajax。

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

#3


0  

 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.

如果您只想重定向当前选项卡,则不要打开新窗口。使用location.href insteat。属性设置器重定向您当前的Tab。