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

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?



The google search URL is basically:[query]

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

<form action="" target="_blank">
    <input name="q">
    <input type="submit">


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

<button id="search">Search</button>
var q = "Testing google search";
document.getElementById('search').onclick = function() {''+q);



Try this

<script type="text/javascript" charset="utf-8">
function search()
    query = 'hello world';
    url ='' + query;,'_blank');

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

Or just

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

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:




The Zend website uses the following

<form method="get" action="//" target="_blank">
    <input type="text" name="q" maxlength="255" placeholder="Search in the site">
    <input type="hidden" name="sitesearch" value="">

This works well. However, I don't know what is the Google policy about this kind of integration as it is providing a forced value (sitesearch)


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.