Google搜索框仅在50%的时间内有效

时间:2011-04-12 15:35:42

标签: javascript forms search

我正在为Firefox编写一个新的标签扩展程序,我想要一个可以输入的框并让它搜索Google。我真的不想使用自定义Google搜索,因为我觉得它不一致而且看起来很便宜。

它应该做的就是将您的查询添加到谷歌URL的末尾(当然是正确的格式)并将您重定向到该页面。我有时会工作,但不是所有的时间。

这是我的代码:

JS:

var textstring;

//Gets the text from the form
function getQ() {
    textstring = document.forms['Search'].elements[0].value;
}

//Does a Google Search
function googleSearch() {
    window.location ="http://www.google.com/search?q="+textstring;
}

//main function to run everything
function main() {
    getQ();
    googleSearch();
}

HTML:

<form name="Search" >
<div id="test1">
    <input type="text" name="q" size="31" maxlength="255" value="" />
</div>
<div id="test2">
    <input type="button" value="Google Search" onclick="main();" />
</div>
</form>

不确定为什么最后/表格没有显示出来,但那就是你们知道的那些。

它占25%的时间。我无法弄清楚它有什么问题。难道只是我在本地测试它吗?我一直在Firefox中测试它,但它似乎在IE或Chrome中也存在同样的问题。

1 个答案:

答案 0 :(得分:1)

为什么不直接提交谷歌(并避免所有的JavaScript )?

<form name="Search" method="get" action="http://www.google.com/search" >

并使用正常的提交按钮,如

<input type="submit" value="Google Search" />

由于您已将输入元素命名为q,并且表单的方法为get,因此它将创建正确的网址。

示例:http://jsfiddle.net/gaby/gxun9/