假设我有一个 Javascript 变量,其中包含几个用空格分隔的搜索词,是否可以使用这些词启动 Google 搜索窗口或选项卡(例如,在用户单击按钮后)?如果是,是否有人有一个简单的代码示例可以注入 HTML?
4 回答
google 搜索网址基本上是:https://www.google.com/search?q=[query]
使用它,您可以使用没有 javascript 的简单表单轻松构建要导航到 f.ex 的搜索 URL:
<form action="http://google.com/search" target="_blank">
<input name="q">
<input type="submit">
</form>
演示:http: //jsfiddle.net/yGCSK/
如果您在 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>
演示:http: //jsfiddle.net/kGBEy/
尝试这个
<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();">
要不就
<form action="http://google.com" method="get" target="_blank">
<input type="text" name="q" id="q" />
<input type="submit" value="search google">
当然只是将带有谷歌搜索参数的链接传递给新窗口/div/ajax div/iframe 但是你不能只打开一个新标签/窗口,这是不允许的,也不推荐。您需要添加一个将打开它的按钮..
Google 搜索参数指南:
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)