如何从JavaScript的新标签或窗口启动谷歌搜索? [英] How to launch a google search in a new tab or window from javascript?
问题描述
假设我有一个Javascript变量,其中包含一些用空格分隔的搜索词,是否可以使用这些术语启动Google搜索窗口或标签(例如用户点击按钮后)?如果是的话,是否有人有一个简单的代码示例注入HTML?
谷歌搜索的URL基本上是:https: //www.google.com/search?q=[query]
使用该功能,您可以使用简单的表单轻松构建搜索网址以导航到f.ex没有javascript:
< form action =http://google.com/searchtarget =_ blank>
< input name =q>
< input type =submit>
< / form>
演示: http://jsfiddle.net/yGCSK/
如果您在JavaScript变量中有搜索查询,如下所示:
< button id =search>搜寻< / button>
< script>
var q =测试Google搜索;
document.getElementById('search')。onclick = function(){
window.open('http://google.com/search?q='+q);
};
< / script>
演示: http://jsfiddle.net/kGBEy/
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: 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/
这篇关于如何从JavaScript的新标签或窗口启动谷歌搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!