Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make onchange dropdown option-url to open in my page

   <html>
<head>
</head>
<body>
<select name="menu1" id="menu1">
<option value="http://www.espn.com">ESPN</option>
<option value="http://www.cnn.com">CNN</option>
<option value="http://www.abcnews.com">ABC</option>
<option value="http://www.cbsnews.com">CBS</option>
<option value="http://www.foxnews.com">FOX</option>
</select>
<script type="text/javascript">
 var urlmenu = document.getElementById( 'menu1' );
 urlmenu.onchange = function() {
      window.open( this.options[ this.selectedIndex ].value );
 };
</script>
</body>
</html>

I have this code and i want to open by click the selected in my page! not open another! thank you!

like image 979
cbour Avatar asked Dec 23 '13 10:12

cbour


2 Answers

Use this :

urlmenu.onchange = function() {
  window.open( this.options[ this.selectedIndex ].value, '_self');
};

window.open(URL,name,specs,replace): where name:

_blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the title of the new window)

DEMO Link

like image 59
brg Avatar answered Sep 22 '22 04:09

brg


Add second parameter _self

<html>
<head>
</head>
<body>
    <select name="menu1" id="menu1">
        <option value="http://www.espn.com">ESPN</option>
        <option value="http://www.cnn.com">CNN</option>
        <option value="http://www.abcnews.com">ABC</option>
        <option value="http://www.cbsnews.com">CBS</option>
        <option value="http://www.foxnews.com">FOX</option>
    </select>
    <script type="text/javascript">
     var urlmenu = document.getElementById( 'menu1' );
     urlmenu.onchange = function() {
          window.open( this.options[ this.selectedIndex ].value, '_self');
     };
    </script>
</body>
</html>
like image 36
Rajnikant Kakadiya Avatar answered Sep 22 '22 04:09

Rajnikant Kakadiya