简体   繁体   English

通过javascript函数预定义谷歌搜索

[英]Predefined google search through javascript function

I need to do a predefined Google Search through a javascript function, launched by a button in the page. 我需要通过javascript函数进行预定义的Google搜索,该功能由页面中的按钮启动。

So I have tryed before with no sucess: 所以我之前尝试过没有成功:

<html>
<head>
<script language="JavaScript">     
  function googleSearch(quest){
   var googlefind = "http://www.google.com/search?hl=en&q=" + quest + " buy Blu-Ray DVD";
   window.open(googlefind);
  }
</script>
</head>

<body>
 <INPUT type="button" value="Buy this Anime Now!" onclick="googleSearch("Fullmetal Alchemist Brotherhood");">
</body>
</html>

Does somebody can help me please? 有人可以帮我吗?

Added escape() to googlefind in your function, and changed the keywords to be in single quotes in your onclick . 在函数中为googlefind添加了escape() ,并将关键字更改为onclick中的单引号。

<html>
<head>
<script language="JavaScript">
 function googleSearch(quest){
  var googlefind = quest + " buy Blu-Ray DVD";
  window.open("http://www.google.com/search?hl=en&q=" + escape(googlefind));
 }
</script>
</head>

<body>
 <INPUT type="button" value="Buy this Anime Now!" onclick="googleSearch('Fullmetal Alchemist Brotherhood');">
</body>
</html>

Also, keep in mind that some browsers will not like the script tag the way it's currently defined. 另外,请记住,某些浏览器不会像目前定义的那样喜欢script标记。 I'd change to type='text/javascript instead of language='JavaScript' like the following: 我将改为type='text/javascript而不是language='JavaScript' ,如下所示:

<html>
<head>
<script type="text/javascript">
 function googleSearch(quest){
  var googlefind = quest + " buy Blu-Ray DVD";
  window.open("http://www.google.com/search?hl=en&q=" + googlefind);
 }
</script>
</head>

<body>
 <INPUT type="button" value="Buy this Anime Now!" onclick="googleSearch('Fullmetal Alchemist Brotherhood');" />
</body>
</html>

在传递参数onclick =“googleSearch('Fullmetal Alchemist Brotherhood')中用单引号替换双引号

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

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