简体   繁体   English

将随机数传递给Google搜索

[英]Passing a random number to Google search

What I want to do is to generate a random number between and creating a button that will google it. 我想做的是在之间生成一个随机数,并创建一个将其谷歌搜索的按钮。 I am stuck on creating the button to do so. 我坚持创建按钮来这样做。

I am using the following code to generate a random number: 我正在使用以下代码生成一个随机数:

<div id="random" onclick="javascript:document.getElementById('random').innerHTML = Math.floor(Math.random() * 1001);" >

How do I add functionality to a button which will Google that randomly generated number? 如何为按钮添加功能,Google会随机生成该数字?

Right now, I am thinking on the lines of how we generate forms, which Google what the user types in them, for instance: 现在,我正在考虑如何生成表单,例如Google在用户中键入的内容:

<form align = "center" action = "http://www.google.com/search"  target="_blank">
    <input name = "q"> <input type = "submit" value="Google !"> 
</form>

You don't really need a form, a regular hyperlink would work just fine. 您实际上并不需要表单,常规超链接可以正常工作。

<a href="javascript: window.open('http://www.google.com/search?q=' + Math.floor(Math.random() * 1001))">Google!</a>

Of course, you could use an image instead of text if you want to use a graphic button, and if you'd like to use a regular form button, the following would do the trick... 当然,如果您想使用图形按钮,则可以使用图像而不是文本,并且,如果您想使用常规的表单按钮,则可以使用以下技巧:

<input type="button" onclick="window.open('http://www.google.com/search?q=' + Math.floor(Math.random() * 1001))" value="Google!" />

You could potentially have the button build a query-string and do a location redirect. 您可能会使按钮建立查询字符串并执行位置重定向。

I think the minimum string for a Google search results page would be something like: 我认为Google搜索结果页的最小字符串应为:

https://www.google.com/search?q=RANDOM_STRING_HERE

Solution with a form 用表格解决

Markup: 标记:

<form align = "center" action = "http://www.google.com/search"  target="_blank">
    <input id='random' type='hidden' name = "q"> 
    <input type = "submit" value="Google !"> 
</form>

Javascript: Javascript:

document.getElementById('random').value = Math.floor(Math.random() * 1001);

This should work. 这应该工作。 :) I tried it out and it went well. :)我尝试了一下,一切顺利。 :) :)

<input type="hidden" name="random" id="random" value="" />
<input type = "submit" onclick="javascript:document.getElementById('random').value =         Math.floor(Math.random() * 1001); window.location ='https://www.google.com/?#hl=en&output=search&q=' +document.getElementById('random').value"value="Google !"> </div>​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM