輸入關鍵字後按「搜一下」,百度與必應的結果會在同一個頁面的上下/左右分割視窗中同時開啟,不必來回切換分頁。
做法是用 frameset 切出三個框架:上方放搜尋表單,下方左右並列兩個結果框架;表單 submit 時,改寫 action 與參數名,分別對兩個 frame 各提交一次。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>同時搜索百度和必應</title>
</head>
<frameset rows="30%,70%">
<frame src="keleyi.htm">
<frameset cols="45%,45%">
<frame id="baiduFrame" name="baiduFrame" src="images/baidu.jpg"></frame>
<frame id="bingFrame" name="bingFrame" src="images/baidu.jpg"></frame>
</frameset>
</frameset>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>同時搜索百度和必應</title>
<script type="text/javascript">
function $(id) {
return document.getElementById(id);
}
function mixSearch() {
var okeyword = $("keyword");
var oForm = $("searchForm");
// 提交給百度:參數名為 wd
oForm.action = "https://www.baidu.com/s";
okeyword.name = "wd";
oForm.method = "get";
oForm.target = "baiduFrame";
oForm.submit();
// 提交給必應:參數名為 q
oForm.action = "https://hk.bing.com/search";
okeyword.name = "q";
oForm.method = "get";
oForm.target = "bingFrame";
oForm.submit();
}
</script>
</head>
<body style="text-align: center;">
<form action="" id="searchForm" method="get" target="baiduFrame">
<input type="text" id="keyword" name="wd" value="" />
<input type="button" value="搜一下~" onclick="mixSearch();" />
</form>
<hr />
</body>
</html>
兩個檔案放在同一個資料夾,直接開啟 index.htm 即可使用。
關鍵在於「同一個 form 可以被 submit 兩次」。第一次送出去之前先把 action 與 input 的 name 改成百度要的樣子,target 指向 baiduFrame;第二次再改成必應要的樣子,target 換成 bingFrame。因為每次 submit 都是完整的一次導航,兩者互不干擾。