Fernando Factor
Fernando Factor

Reputation: 3

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.

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?

Upvotes: 0

Views: 913

Answers (3)

aravindKrishna
aravindKrishna

Reputation: 440

replace double quotes with single quotes in passing parameter onclick="googleSearch('Fullmetal Alchemist Brotherhood')

Upvotes: 0

MilkyWayJoe
MilkyWayJoe

Reputation: 9092

Also, keep in mind that some browsers will not like the script tag the way it's currently defined. I'd change to type='text/javascript instead of language='JavaScript' like the following:

<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>

Upvotes: 0

Nadh
Nadh

Reputation: 7253

Added escape() to googlefind in your function, and changed the keywords to be in single quotes in your 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>

Upvotes: 2

Related Questions