0

Problem: to open a new window with the select -option

<form onsubmit="return handleSubmit()" target="_blank" method="get" name="moduleForm" id="moduleForm">
<font size=2 face = verdana color= #0000ff ><b>Search</b></font>

    <select name="allSelect" id="allSelect">
    <optgroup label="Historical">
    <option value="http://www.something.com/cse?cx=0000000000000&sa=Search&q=">Open in a new window 1</option>
    <option value="http://www.google.com/cse?cx=0000000000000000A-cmjrngmyku&ie=UTF-8&sa=Search&q=">Open in a new window 2</option>
    </optgroup>
    </select>

<input type="text" name="allQuery" id="allQuery" size="22" />
<input type="submit" value=" Go " />

Question: How can I open the content to a new window with a select-box?

Canavar
  • 47,715
  • 17
  • 91
  • 122
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

4 Answers4

1

You can open your links by window.open() :

<select name="allSelect" id="allSelect">
<optgroup label="Historical">
<option value="http://www.something.com/cse?cx=0000000000000&sa=Search&q=">Open in a new window 1</option>
<option value="http://www.google.com/cse?cx=0000000000000000A-cmjrngmyku&ie=UTF-8&sa=Search&q=">Open in a new window 2</option>
</optgroup>
</select>

<input type="button" 
    value="open in a new window" 
    onclick="window.open(document.getElementById(allSelect).value);" />
Canavar
  • 47,715
  • 17
  • 91
  • 122
1

Give a look to the window.open function.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

Keep in mind that your page should be usable without scripting, so I'd suggest implementing a fallback mechanism: The form should call a server-side script which responds with a 30x status and a Location header.

The client-side would look like this:

<form action="path-to-redirection-script" method="GET" target="_blank"
 onsubmit="window.open(this.elements['foo'].value); return false;">
 <select name="foo" size="1">
  <option value="http://google.com">google</option>
 </select>
 <input type="submit" value="go">
</form>

Also, remember that target="_blank" / window.open() is often evil.

Christoph
  • 164,997
  • 36
  • 182
  • 240
1

Modify your handleSubmit function as follows:

function handleSubmit()
{
    var form = _gel("moduleForm"),
        elm = _gel("allQuery"),
        selectElm = _gel("allSelect");
    if (elm != "" && selectElm != "") {
        var query = elm.value;
        var searchUrl = selectElm.value;
        if (query != "" && searchUrl != "") {
            searchUrl += escape(query);
            window.open(searchUrl, form.target || "_blank");
        }
    }
    return false;
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • How did you know about the parts 'form = _gel("moduleForm");' and 'form.target || "_blank")'? I cannot even google them. – Léo Léopold Hertz 준영 Jul 12 '09 at 21:10
  • Great thanks for solving the problem. Sorry for being straightforward, but how on earth you you knew about the "moduleForm"-thing? It amazes me. Hope to be as good as you some day. Thanks again. – Léo Léopold Hertz 준영 Jul 12 '09 at 21:13
  • 1
    That’s just the form’s name/ID in your example code. And after the `_gel()` function is also used for the input field `allQuery` and select field `allSelect`, I assumed that this function uses that value either as name or ID. And `form.target || "_blank"` is just an OR expression that returns the value of the first alternation that’s can evaluated to *true*. So if `form.target` would be undefined, `"_blank"` would be used as default value. – Gumbo Jul 12 '09 at 21:23
  • Can you clarify the sentence "That’s just the form’s name/ID in your example code."? I cannot find it from the source code. – Léo Léopold Hertz 준영 Jul 12 '09 at 21:35
  • Sure. First line: `
    `**
    – Gumbo Jul 12 '09 at 21:37
  • Gumbo: Please, see my view: http://pastebin.com/d6bfd694. There is no such line. I tested it with Opera, Firefox and Safari. I suppose you mean this as my example: http://savedbythegoog.appspot.com/?id=ag5zYXZlZGJ5dGhlZ29vZ3ISCxIJU2F2ZWRDb2RlGNawvAEM. I cannot see no such line. – Léo Léopold Hertz 준영 Jul 12 '09 at 21:42
  • 1
    The form’s start tag is missing in that example document. But it’s in the example code in your question. – Gumbo Jul 12 '09 at 21:44
  • Solved. Thank you Gumbo! I did not notice my mistake in the example. Sorry for the inconvience. – Léo Léopold Hertz 준영 Jul 12 '09 at 21:47