admin管理员组文章数量:1200980
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?
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?
Share Improve this question asked May 20, 2013 at 12:06 Jérôme VerstryngeJérôme Verstrynge 59.6k95 gold badges295 silver badges466 bronze badges 1 |4 Answers
Reset to default 15The 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/
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">
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/
The Zend website uses the following
<form method="get" action="//www.google.com/search" target="_blank">
<input type="text" name="q" maxlength="255" placeholder="Search in the site">
<input type="hidden" name="sitesearch" value="framework.zend.com">
</form>
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)
本文标签: htmlHow to launch a google search in a new tab or window from javascriptStack Overflow
版权声明:本文标题:html - How to launch a google search in a new tab or window from javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738563310a2099659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
window.open()
or ,window.open(url, '_blank')
– Pete Commented May 20, 2013 at 12:16