忽然想到,若能在自己的網頁裡直接呼叫 Google、百度這類強大的搜尋引擎,想必很酷。上網查了資料,沒想到只是寥寥幾行程式碼就能辦到。以下以百度為例示範:
<!-- 在頁面中呼叫百度引擎 -->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>呼叫百度引擎</title>
</head>
<body>
<div id="container">
<form action="https://www.baidu.com/baidu" target="_blank">
<div align="center">
<input name="tn" type="hidden" value="baidu">
<a href="https://www.baidu.com/">
<img src="https://img.baidu.com/search/img/baidulogo_clarity_80_29.gif" alt="Baidu" align="bottom" border="0"></a>
<input type="text" name="word" size="30">
<input type="submit" value="百度搜尋">
</div>
</form>
</div>
</body>
</html>
原理其實很單純:把表單的 action 指向百度的搜尋網址,使用者在輸入框(name="word")中輸入關鍵字後送出,就會開啟一個新視窗(target="_blank")並顯示搜尋結果。是不是比想像中簡單多了?