JS实现同时搜索百度和必应的方法

作者:半夏时间:2026-10-28 20:50:19浏览:169

本文介绍一种基于HTML frameset和JavaScript的简单方案,实现在同一页面同时向百度和必应发起搜索请求。通过构造表单并分别指定target指向不同的frame,用户输入关键词后点击按钮,即可在左右两个窗口中分别显示两个搜索引擎的结果,方便进行对比查找。

同时调用百度与必应搜索的实现原理

本文介绍一种利用JavaScript实现同时调用百度和必应搜索引擎的方法,供开发者参考。

该方案的核心功能是:当用户输入关键词并点击“搜一下”按钮时,系统会同时向百度和必应发起请求,两个搜索引擎的结果将在同一个页面内的不同区域打开,便于用户直观对比。

实现此功能需要保存两个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>
<meta http-equiv="keywords" content="同时搜索百度和必应">
<meta http-equiv="description" content="同时搜索百度和必应">
</head>
<!--<body style="text-align: center;">
-->
<frameset rows="30%,70%">
<frame src="keleyi.htm">
</frame>
<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>
<!--<iframe id="baiduFrame" width="49%" height="100%" ></iframe>
<iframe id="bingFrame" width="49%" height="100%" ></iframe>
</body>-->
</html>

主框架页面index.htm结构

第二个文件的名称为keleyi.htm。

搜索交互页面keleyi.htm逻辑

该文件的代码内容如下:

代码实现如下:

<!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>
<meta http-equiv="keywords" content="同时搜索百度和必应">
<meta http-equiv="description" content="同时搜索百度和必应">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">
<script type="text/javascript">
function $(id) {
return document.getElementById(id);
}
function mixSearch() {
var okeyword = $("keyword");
var oForm = $("searchForm");
//提交给百度,构造地址参数
oForm.action = "http://www.baidu.com/s";
okeyword.name = "wd";
oForm.method = "get";
oForm.target = "baiduFrame";
oForm.submit();
//提交给bing,构造地址参数
oForm.action = "http://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>

希望本文所述的JavaScript程序设计示例能对读者有所帮助。

作者最新文章