Change to URL X

Change URL with html select

What is the best way to change the url using html select?

<select>
<option selected="selected">Change to URL X</option>
<option>Change to URL Y</option>
</select>

Which Javascript should I use?

+5
source share
3 answers
<script type="text/javascript">
function navigateTo(sel, target, newWindow) {
    var url = sel.options[sel.selectedIndex].value;
    if (newWindow) {
        window.open(url, target, '--- attributes here, see below ---');
    } else {
        window[target].location.href = url;
    }
}
</script>

<select onchange="navigateTo(this, 'window', false);">
<option selected="selected" value="http://www.example.com/#X">Change to URL X</option>
<option value="http://www.example.com/#Y">Change to URL Y</option>
</select>

Some useful values targetmay be 'window'(current window) or 'top'(to exit a set of frames or iframes). If you want to open a new window instead, you can usenavigateTo(this, 'someWindow', true);

The value '--- attributes ---' is set using various properties, as described here for Mozilla and here for IE . For example:

'height=300,width=400,top=100,left=100,statusbar=0,toolbar=1'
+6
source

jQuery, ...

JavaScript:

$('# select_url'). change (function (evnt) {location.href= $(this).val();});

HTML:

...

+5

, , -

<script language="JavaScript" type="text/javascript">
function jumpMenu(targ,selObj,restore){ 
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
}
</script>

and the selection window looks like this:

<select onchange="jumpMenu('parent',this)>
<option selected="selected">Change to URL X</option>
<option value="http://www.example.com">Change to URL Y</option>
</select>
-3
source

All Articles