استخدام JavaScript لإنشاء شريط أدوات البحث
Zhou Yinhui No. 1, Class 03, School of Software, University of Electronic Science and Technology of China
: التأثير النهائي
2: المبدأ
إذا بحثت عن "الصين" في Yahoo، فسوف تحصل على سلسلة من العناوين مثل هذه في شريط العناوين بالمتصفح: http://search.cn.yahoo.com/search?ei=gbk&fr=fp- علامة التبويب ويب -ycn&meta=vl%
3Dlang_zh-CN%26vl%3Dlang_zh-TW&pid=ysearch&source=ysearch_www_hp_button
&p=%D6%D0%B9%FA&إرسال=
يبدو الأمر فوضويًا بعض الشيء، فلنبسطه: http://search.cn.yahoo.com/search?&p=%D6%D0%B9%FA
هذا هو المفتاح حيث أن &p=%D6%D0%B9%FA هي معلمة الكلمة الرئيسية للبحث، و%D6%D0%B9%FA هي "الصين".
ترميز URL. حسنًا، طالما أننا نستطيع إنشاء مثل هذا التشفير.
ثلاثة: ترميز URL
يمكن لوظيفة encodeURIComponent() الخاصة بجافا سكريبت إكمال أعمال الترميز.
على سبيل المثال، في المثال أعلاه يمكننا استخدام "http://search.cn.yahoo.com/search?&p="+encodeURIComponent("China");
الرابع: الكود
(انقر فوق علامة الجمع للتوسيع)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
4
5 <html xmlns=" http://www.w3.org/1999/xhtml " >
6 <رئيس التشغيل = "الخادم">
7 <title>بحث</title>
8 <script language="javascript" type="text/javascript">
9 // <!CDATA[
10
11 وظيفة GetEncodeOfKey()
12 {
13 var strKey = window.document.getElementById("Text_Key").value;
14 return encodeURIComponent(strKey);
15}
16
17 وظيفة GetUrl(الموقع)
18 {
19 var encode=GetEncodeOfKey();
20
21 //ويب
22 إذا (document.getElementById("RadioButtonList_Kind_0").فحص)
ثلاثة وعشرين {
24 إذا (الموقع = = "جوجل")
25 {
26 إرجاع " http://www.google.com/search?q="+encode+"&ei=UTF-8 ";
27}
28 آخر
29 {
30 إرجاع " http://search.yahoo.com/search?p="+encode+"&ei=UTF-8 ";
31}
32}
33 //mp3
34 آخر إذا (document.getElementById("RadioButtonList_Kind_1").checked)
35 {
36 إذا (الموقع = = "جوجل")
37 {
38 return " http://www.google.com/search?q="+encode +" mp3"+"&ei=UTF-8";
39 }
40 آخر
41 {
42 return " http://audio.search.yahoo.com/search/audio?&p="+encode+"&ei=UTF-8 ";
43
44}
45 }
46 //img
47 آخر إذا (document.getElementById("RadioButtonList_Kind_2").checked)
48 {
49 إذا (الموقع = = "جوجل")
50 {
51 إرجاع " http://images.google.com/images?q="+encode+"&ei=UTF-8 ";
52 }
53 آخر
54 {
55 إرجاع " http://images.search.yahoo.com/search/images?p="+encode+"&ei=UTF-8 ";
56 }
57 }
58 آخر
59 {
60 //تنبيه("يخطئ");
61 }
62
63
64}
65
66 وظيفة Button_Google_onclick()
67 {
68 window.open(GetUrl("google"));
69 }
70
71 وظيفة Button_Yahoo_onclick()
72 {
73 window.open(GetUrl("yahoo"));
74}
75
76 // ]]>
77 </script>
78 </الرأس>
79 <الجسم>
80 <form id="form1" runat="server">
81 <ديف>
82 <br />
83 <br />
84 <strong><span style="font-size: 24pt; color: #336633">بحث<br />
85 </span></strong>
86 </ديف>
87 <hr style="position: نسبي" />
88 <br />
89 <نمط الجدول = "يسار: 0 بكسل؛ الموضع: نسبي؛ الأعلى: 0 بكسل">
90<تر>
91 <td style="width: 31px; الارتفاع: 21px">
92 <span style="font-family: Terminal">المفتاح</span></td>
93 <td style="width: 253px; الارتفاع: 21px">
94 <input id="Text_Key" style="width: 248px; الموضع: نسبي" type="text" /></td>
95 <td style="width: 175px; الارتفاع: 21px">
96 <asp:RadioButtonList ID="RadioButtonList_Kind" runat="server" RepeatDirection="Horizontal"
97 النمط = "الموضع: نسبي" أسماء الخطوط = "المحطة">
98 <asp:ListItem Selected="True">الويب</asp:ListItem>
99 <asp:ListItem>Mp3</asp:ListItem>
100 <asp:ListItem>صورة</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; الموضع: نسبي" type="button" value="Google" onclick="return Button_Google_onclick()" />
108
109 <معرف الإدخال = "Button_Yahoo" نمط = "يسار: -29px؛ العرض: 104px؛ الموضع: نسبي" نوع = "زر"
110 value="Yahoo onclick="return Button_Yahoo_onclick()" /></td>
111 </tr>
112 </الجدول>
113 <br />
114 <hr style="position: نسبي" />
115 <asp:HyperLink ID="HyperLink_Home" runat="server" NavigateUrl="~/Default.aspx" Style="position: النسبي">الصفحة الرئيسية</asp:HyperLink></form>
116 </الجسم>
117</html>
118