国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

用JavaScript打造搜索工具欄

2019-11-18 17:10:47
字體:
來源:轉載
供稿:網友

javaScript打造搜索工具欄

電子科技大學軟件學院03級2班 周銀輝

一:最終效果

二:原理

如果你在Yahoo中搜索“中國”,那么在瀏覽器的地址欄將得到這樣一串地址:http://search.cn.yahoo.com/search?ei=gbk&fr=fp-tab-web-ycn&meta=vl%
3Dlang_zh-CN%26vl%3Dlang_zh-TW&pid=ysearch&source=ysearch_www_hp_button
&p=%D6%D0%B9%FA&Submit=
看上去有些亂了,簡化一下:http://search.cn.yahoo.com/search?&p=%D6%D0%B9%FA
這就是關鍵.其中&p=%D6%D0%B9%FA是搜索的關鍵字參數,而%D6%D0%B9%FA是“中國”的
Url編碼。OK,我們只要能構造出這樣的編碼就好了。

三:URL編碼
Javascript的encodeURIComponent()函數可以完成編碼工作。
比如上面的例子我們可以用“http://search.cn.yahoo.com/search?&p=”+encodeURIComponent(“中國”);來完成。

四:代碼
(點擊加號展開)

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
  2
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "  4
  5 <html
xmlns="  6 <head runat="server">
  7     <title>Search</title>
  8 <script language="javascript" type="text/javascript">
  9 // <!CDATA[
 10
 11 function GetEncodeOfKey()
 12 {
 13     var strKey = window.document.getElementById("Text_Key").value;     
 14     return  encodeURIComponent(strKey);
 15 }
 16
 17 function GetUrl(site)
 18 {
 19     var encode=GetEncodeOfKey();
 20    
 21     //web
 22     if(document.getElementById("RadioButtonList_Kind_0").checked)
 23     {
 24         if(site=="google")
 25         {
 26             return "
 27         }
 28         else
 29         {
 30             return "
 31         }
 32     }
 33     //mp3
 34     else if(document.getElementById("RadioButtonList_Kind_1").checked)
 35     {
 36         if(site=="google")
 37         {
 38             return "
 39         }
 40         else
 41         {
 42             return "
 43           
 44         }
 45     }
 46     //img
 47     else if(document.getElementById("RadioButtonList_Kind_2").checked)
 48     {
 49         if(site=="google")
 50         {
 51             return "
 52         }
 53         else
 54         {
 55             return "
 56         }
 57     }
 58     else
 59     {
 60         //alert("err");
 61     }
 62    
 63    
 64 }
 65
 66 function Button_Google_onclick()
 67 {
 68     window.open(GetUrl("google"));
 69 }
 70
 71 function Button_Yahoo_onclick()
 72 {
 73     window.open(GetUrl("yahoo"));
 74 }
 75
 76 // ]]>
 77 </script>
 78 </head>
 79 <body>
 80     <form id="form1" runat="server">
 81     <div>
 82         <br />
 83         <br />
 84         <strong><span style="font-size: 24pt; color: #336633">Search<br />
 85         </span></strong>
 86     </div>
 87     <hr style="position: relative" />
 88         <br />
 89         <table style="left: 0px; position: relative; top: 0px">
 90             <tr>
 91                 <td style="width: 31px; height: 21px">
 92                     <span style="font-family: Terminal">Key</span></td>
 93                 <td style="width: 253px; height: 21px">
 94                     <input id="Text_Key" style="width: 248px; position: relative" type="text" /></td>
 95                 <td style="width: 175px; height: 21px">
 96                     <asp:RadioButtonList ID="RadioButtonList_Kind" runat="server" RepeatDirection="Horizontal"
 97                         Style="position: relative" Font-Names="terminal">
 98                         <asp:ListItem Selected="True">Web</asp:ListItem>
 99                         <asp:ListItem>Mp3</asp:ListItem>
100                         <asp:ListItem>Image</asp:ListItem>
101                     </asp:RadioButtonList></td>
102             </tr>
103             <tr>
104                 <td style="width: 31px">
105                 </td>
106                 <td colspan="2">
107                     <input id="Button_Google" style="width: 80px; position: relative" type="button" value="Google" onclick="return Button_Google_onclick()" />
108                     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
109                     <input id="Button_Yahoo" style="left: -29px; width: 104px; position: relative" type="button"
110                         value="Yahoo!" onclick="return Button_Yahoo_onclick()" /></td>
111             </tr>
112         </table>
113         <br />
114         <hr style="position: relative" />
115         <asp:HyperLink ID="HyperLink_Home" runat="server" NavigateUrl="~/Default.aspx" Style="position: relative">Home</asp:HyperLink></form>
116 </body>
117 </html>
118


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华阴市| 镇雄县| 正镶白旗| 长顺县| 盐山县| 深州市| 容城县| 东莞市| 浙江省| 邓州市| 宁河县| 合江县| 册亨县| 安国市| 资兴市| 英德市| 寿阳县| 镇沅| 那曲县| 兴仁县| 高阳县| 赣州市| 高淳县| 荔波县| 榆林市| 南阳市| 襄城县| 公安县| 广饶县| 会泽县| 雷山县| 分宜县| 建瓯市| 沾益县| 德兴市| 合作市| 泰来县| 桃江县| 麦盖提县| 潍坊市| 上思县|