I am struggling with creating a form that enables a user to input text and then directs them to a specified search engine with the input as the query. I am encountering difficulties in getting the JavaScript to properly redirect.
An interesting observation is that if you change the submit input to a button, add onClick="searchForm", and manually click on it, the functionality works. However, this method eliminates the ability to simply type and press enter for searching, which I believe is essential.
If there are alternative methods of achieving this functionality, I would greatly appreciate any suggestions.
Thank you.
EDIT: I have completely revised the code based on meouw's suggestion, as it was much clearer than my initial approach. The HTML structure was jumbled up due to multiple modifications aimed at resolving the JavaScript issue. Even after organizing everything, the problem persists.
I am aware that something crucial is missing because inserting an alert('cat'); after changing the URL in the JavaScript reveals that the browser does indeed redirect for the search. However, upon clicking OK on the alert, the redirection is reversed. It is even possible to navigate back through the browser history to return to the search page.
EDIT AGAIN: Thank you, meouw. Adding 'return false' was the solution I had been overlooking. Your assistance is valued.
FINAL CODE:
<script type="text/javascript">
function doSearch( f ) {
var searchTerm = f.searchText.value;
if( !searchTerm ) {
//prompt user to enter a search string
//cancel the request by returning false
return false;
}
var sel = f.whichEngine;
var selectedEngine = sel[sel.selectedIndex].value;
var engineUrl;
switch( selectedEngine ) {
case 'google_web':
engineUrl = 'http://www.google.com/search?q=';
break;
case 'bing_web':
engineUrl = 'http://www.bing.com/search?q=';
break;
case 'yahoo_web':
engineUrl = 'http://search.yahoo.com/search?p=';
break;
case 'google_images':
engineUrl = 'http://www.images.google.com/images?q=';
break;
case 'bing_images':
engineUrl = 'http://www.bing.com/images/search?q=';
break;
case 'yahoo_images':
engineUrl = 'http://images.search.yahoo.com/search/images?p=';
break;
}
engineUrl += searchTerm;
window.location.assign(engineUrl);
return false;
}
</script>
<form onsubmit="return doSearch(this)">
<fieldset>
<legend>Search</legend>
<ul>
<li>
<input type="text" name="searchText" id="searchText" size="41" maxlength="2048" />
</li>
<li>
<select name="whichEngine" id="whichEngine">
<optgroup label="Web">
<option id="GoogleWeb" value="google_web" checked="checked">Google Web</option>
<option id="BingWeb" value="bing_web">Bing Web</option>
<option id="YahooWeb" value="yahoo_web">Yahoo! Web</option>
</optgroup>
<optgroup label="Images">
<option id="GoogleImages" value="google_images">Google Images</option>
<option id="BingImages" value="bing_images">Bing Images</option>
<option id="YahooImages" value="yahoo_images">Yahoo! Images</option>
</optgroup>
</select>
<input type="submit" value="Search">
</li>
</ul>
</fieldset>
</form>