如何从 javascript 在新选项卡或窗口中启动谷歌搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16649167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to launch a google search in a new tab or window from javascript?
提问by Jér?me Verstrynge
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?
假设我有一个 Javascript 变量,其中包含几个由空格分隔的搜索词,是否可以使用这些词来启动 Google 搜索窗口或选项卡(例如,在用户单击按钮后)?如果是,是否有人有一个简单的代码示例可以注入 HTML?
回答by David Hellsing
The google search URL is basically: https://www.google.com/search?q=[query]
谷歌搜索网址基本上是: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:
使用它,您可以轻松构建要导航到的搜索 URL,f.ex 使用不带 javascript 的简单表单:
<form action="http://google.com/search" target="_blank">
<input name="q">
<input type="submit">
</form>
Demo: http://jsfiddle.net/yGCSK/
演示:http: //jsfiddle.net/yGCSK/
If you have the search query in a javascript variable, something like:
如果您在 javascript 变量中有搜索查询,例如:
<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>
回答by Dinever
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">
回答by Dory Zidon
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..
当然只需将带有 google 搜索参数的链接传递到新窗口/div/ajax div/iframe 但是您不能只打开一个新选项卡/窗口,这是不允许也不推荐的。你需要添加一个按钮来打开它..
Guide to Google Search Parameters:
Google 搜索参数指南:
1)http://www.seomoz.org/ugc/the-ultimate-guide-to-the-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/
2) http://www.blueglass.com/blog/google-search-url-parameters-query-string-anatomy/
回答by Xavier13
The Zend website uses the following
Zend 网站使用以下内容
<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)
这很好用。但是,我不知道关于这种集成的 Google 政策是什么,因为它提供了强制价值(站点搜索)