Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:54:28+00:00 2026-05-13T16:54:28+00:00

I’m trying to make a form that will take given text and direct you

  • 0

I’m trying to make a form that will take given text and direct you to a chosen search engine with the text as the query. I’m having problems getting the javascript to (re)direct.

What’s interesting is that if you make the submit input a button and do onClick=”searchForm” and then click on it, it will work. But then you can’t type and just press enter to search.. which to me is necessary.

Also, if there is any better but completely different way to do this, I’d love to hear it.

Thanks.

EDIT: Completely changed the code to match what meouw said, which was much more clear than what I originally had, so thanks. I know the HTML was messed up, it was mostly because I was changing everything around to try and get the JavaScript to work. So even now that I cleared everything up, it still has the same problem.

I know there is something I am missing, because if you put an alert(‘cat’); after changing the url in the JavaScript, you can see that the browser has been redirected to do the search, but once you click OK on the alert, you get taken back. You can even go back in your browser’s history to go to your search’s page.

EDIT AGAIN: Ok, thanks meouw. Returning false was what I needed all along. Thanks for the help.

FINAL CODE:

<script type="text/javascript">
    function doSearch( f ) {
        var searchTerm = f.searchText.value;

        if( !searchTerm ) {
            //tell 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:54:28+00:00Added an answer on May 13, 2026 at 4:54 pm

    Your main problem is this

    window.location.assign(finalSearchString);
    

    should be

    window.location = finalSearchString;

    window.location.href = finalSearchString;
    

    Other things:

    Your markup is odd
    You need to return the output of your function to the onsubmit of the form in case you want to cancel the search.
    You don’t need the labels and links in the options – instead of having self closing option tags put the text in the option and give the option a value that can be read by your script. This is safer if you come to adding more options later

    <form onsubmit="return doSearch(this)">
    ....
    <optgroup label="Google">
        <option value="google_search">Google Web</option>
        <option value="google_images">Google Images</option>
    </optgroup>
    ....
    </form>
    
    <script type="text/javascript">
    
    function doSearch( f ) {
        var searchTerm = f.searchText.value;
    
        if( !searchTerm ) {
            //tell 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_search':
                engineUrl = 'http://www.google.com/search?q=';
                break;
        ....
        }
    
        engineUrl += searchTerm;
        window.location.href = engineUrl;
    
    }
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.