4

This works but I require it to open the link in a new window:

<select onChange="window.location=this.value">
<option value ="">select</option> 
<option value="http://www.google.com">google</option>
</select>

(I note this has been shown using a button and form but I require it to happen by selecting only)

gungu
  • 161
  • 1
  • 1
  • 9

2 Answers2

7

Have you tried the window.open?

<select onChange="window.open(this.value)">
<option value ="">select</option> 
<option value="http://www.google.com">google</option>
</select>

https://jsfiddle.net/m9ts7tnj/

hack3rfx
  • 573
  • 3
  • 16
  • thanks but – gungu Jan 19 '16 at 03:34
  • you have to use parenthesis window.open(url) – hack3rfx Jan 19 '16 at 03:37
  • please explain in the context of the select statement – gungu Jan 19 '16 at 03:39
  • What environment are you testing this in? – hack3rfx Jan 19 '16 at 03:48
  • 1
    Every sample provided thus far works. Do you have pop-ups blocked? What browser are you using? – hack3rfx Jan 19 '16 at 03:50
  • safari 9.0 on mac os 10.11 – gungu Jan 19 '16 at 03:51
  • I'm not familiar with Mac, but try this maybe? safari.self.browserWindow.openTab(); – hack3rfx Jan 19 '16 at 03:51
  • yes, popups were blocked but that didn't prevent target='_blank' from working. when i unblocked popups it worked. thank you. this is a new question - is there a way for this to work without having to unblock popups? – gungu Jan 19 '16 at 03:55
  • 1
    Use onclick. Read here.... http://stackoverflow.com/questions/12247368/javascript-window-open-function-opens-link-without-popup-blocker – hack3rfx Jan 19 '16 at 03:57
  • That post also has the correct way of triggering a click event. Rather than clicking directly on the select menu (which should just open it), you should set the href on an invisible a tag (either append it, or just hide it) and then trigger its click event. – Justin Mitchell Jan 19 '16 at 04:26
  • @Mark I'm going to guess that you didn't read the question. The question wasn't regarding an alternative method, but rather how to get the OnChange event to work. This answer satisfied the original request. While your comment has value perhaps, it would be better on the OP post. Additionally, your down-vote was unjustified and superfluous because of the aforementioned reasons. – hack3rfx Jun 08 '17 at 18:35
  • @hack3rfx the downvote is valid because the onchange event will not work due to popup blocker. You can't say, "well, it works except for popup blocker" because in a real world situation, you can't specify whether users in the field will or won't have popup blocker enabled. I'm deleting my previous comment because it turns out even that doesn't work. – Mark Jun 08 '17 at 19:01
  • For anyone who comes here trying to solve this for real world applications, due to the fact that chrome will only click events to trigger a window.open. The solution I ended up using was to use an "apply" button to trigger the selected option. onchange on the select will not work as this is not a mouse event. Option change takes some ms after mouseup. I trired onclick or mouseup on select and waiting for the onchange event to happen before opening the window, chrome engaged the popup blocker. I tried using a hidden link tag and triggering that; but, was unsuccessful. YMMV with that technique – Mark Jun 08 '17 at 19:04
2

You should pass the arguments through a function for extensibility and generally good coding practices.

JS Fiddle: https://jsfiddle.net/51hshn1h/

function openWindow(select) {
    var value = select.options[select.selectedIndex].value;
    window.open(value, 'newwindow')
}

<select onchange="openWindow(this)">
    <option value="">select an option</option>
    <option value="http://google.com">Google</option>
    <option value="http://yahoo.com">Yahoo!</option>
</select>
Justin Mitchell
  • 665
  • 6
  • 13
  • thank you, this works also. Ive used as per Drew's suggestion to bypass the popup blocking issue I was having – gungu Jan 19 '16 at 04:06
  • That's fine, but onClick on a select menu isn't the semantically correct way of doing it. To get around popup blockers, you should set the href of an a tag then trigger the click event on the a tag (and set the target to _blank). – Justin Mitchell Jan 19 '16 at 04:27