2012-04-30 14 views
0

我需要通過JavaScript函數執行預定義的Google搜索,該函數由頁面中的按鈕啓動。通過javascript函數預定義google搜索

所以我一直沒有sucess前tryed:

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

是否有人能幫助我嗎?

回答

2

在您的功能中添加了escape()googlefind,並將關鍵字更改爲您的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> 
+0

問題,費爾南多,是你的雙引號http://jsfiddle.net/ZLQ3P/ –

+0

真棒,你做到了!謝謝你的幫助! –

0

另外,請記住,某些瀏覽器不會像script標籤它目前定義方式。我會改變對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> 
+0

好吧,我明白了,這是另一個很好的示例,謝謝。 –

0

替換單引號雙引號中的參數傳遞 的onclick =「的GoogleSearch(‘鋼之鍊金術師兄弟’)

相關問題