How to launch a google search in a new tab or window from javascript?

20,827

Solution 1

The google search URL is basically: https://www.google.com/search?q=[query]

Using that you can easily build a search URL to navigate to, f.ex using a simple form without javascript:

<form action="http://google.com/search" target="_blank">
    <input name="q">
    <input type="submit">
</form>

Demo: http://jsfiddle.net/yGCSK/

If you have the search query in a javascript variable, something like:

<button id="search">Search</button>
<script>
var q = "Testing google search";
document.getElementById('search').onclick = function() {
    window.open('http://google.com/search?q='+q);
};
</script>

Demo: http://jsfiddle.net/kGBEy/

Solution 2

Try this

<script type="text/javascript" charset="utf-8">
function search()
{
    query = 'hello world';
    url ='http://www.google.com/search?q=' + query;
    window.open(url,'_blank');
}
</script>

<input type="submit" value="" onclick="search();">

Or just

<form action="http://google.com" method="get" target="_blank">
<input type="text" name="q" id="q" />
<input type="submit" value="search google">

Solution 3

Sure just pass a link with google search parameters to a new window / div / ajax div / iframe However you cannot just open a new tab / window, this is not allowed and not recommended. You need to add a button that will open it..

Guide to Google Search Parameters:

1)http://www.seomoz.org/ugc/the-ultimate-guide-to-the-google-search-parameters

2)http://www.blueglass.com/blog/google-search-url-parameters-query-string-anatomy/

Share:
20,827
Jérôme Verstrynge
Author by

Jérôme Verstrynge

You can contact me via my LinkedIn profile.

Updated on July 09, 2022

Comments

  • Jérôme Verstrynge
    Jérôme Verstrynge almost 2 years

    Say I have a Javascript variable containing a couple of search terms separated by spaces, is it possible to start a Google Search window or tab using these terms (after a user clicks on a button for example)? If yes, does anyone have a simple code example to inject in a HTML?

  • twoleggedhorse
    twoleggedhorse almost 11 years
    To add to this, the [query] bit is each search term seperated by the plus (+) symbol. So searching for "test terms" would result in "google.com/search?q=test+terms"
  • David Hellsing
    David Hellsing almost 11 years
    @twoleggedhorse you don’t need the + — spaces will do: google.com/search?q=search%20term