views:

979

answers:

4

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&amp;sa=Search&amp;q="&gt;Open in a new window 1</option>
    <option value="http://www.google.com/cse?cx=0000000000000000A-cmjrngmyku&amp;ie=UTF-8&amp;sa=Search&amp;q="&gt;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?

+1  A: 

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&amp;sa=Search&amp;q="&gt;Open in a new window 1</option>
<option value="http://www.google.com/cse?cx=0000000000000000A-cmjrngmyku&amp;ie=UTF-8&amp;sa=Search&amp;q="&gt;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
+1  A: 

Give a look to the window.open function.

CMS
+1  A: 

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"&gt;google&lt;/option&gt;
 </select>
 <input type="submit" value="go">
</form>

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

Christoph
+1 good points!
Masi
Can you make it such a way that you run the server-side script if and only if the user has JS disabled? If I understand right, the script will always be fetched once per visit.
Masi
The solution to my question in this comment seems to be here: http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled/125775#125775
Masi
I opened a new question here: http://stackoverflow.com/questions/1122609/javascript-for-a-form-on-the-server-side
Masi
+1  A: 

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
How did you know about the parts 'form = _gel("moduleForm");' and 'form.target || "_blank")'? I cannot even google them.
Masi
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.
Masi
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
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.
Masi
Sure. First line: `<form […]` **`name="moduleForm" id="moduleForm">`**
Gumbo
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.
Masi
The form’s start tag is missing in that example document. But it’s in the example code in your question.
Gumbo
Solved. Thank you Gumbo! I did not notice my mistake in the example. Sorry for the inconvience.
Masi