Javascript
Computer Science and Software Institute 03 Class 2 Zhou Banhui
One: the ultimate effect
Two: Principle
If you search for "China" in Yahoo, you will get such a list of addresses in the browser's address bar: 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=
Looks a little messy, simplify: Http://search.cn.yahoo.com/search?&p=%D6%D0%B9%FA
That's the key. Where &P=%D6%D0%B9%FA is the keyword parameter of the search, and%d6%d0%b9%fa is the "China"
URL encoding. OK, all we need to do is construct this code.
Three: URL encoding
The encodeURIComponent () function of JavaScript can do the coding work.
For example, we can use "http://search.cn.yahoo.com/search?&p=" +encodeuricomponent ("China") to complete.
Four: Code
(Click the plus sign to expand)
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 6 7 <title>Search</title>
8 <script language= "JavaScript" type= "Text/javascript" >
9//<! cdata[
10
One function Getencodeofkey ()
12 {
var strkey = window.document.getElementById ("Text_key"). Value;
Return encodeURIComponent (strkey);
15}
16
GetUrl function (site)
18 {
var encode=getencodeofkey ();
20
//web
if (document.getElementById ("Radiobuttonlist_kind_0"). Checked)
23 {
if (site== "Google")
25 {
"http://www.google.com/search?q=" +encode+ "&ei=utf-8";
27}
Else
29 {
Return "http://search.yahoo.com/search?p=" +encode+ "&ei=utf-8";
31}
32}
//mp3
Or else if (document.getElementById ("Radiobuttonlist_kind_1"). Checked)
35 {
if (site== "Google")
37 {
Return "http://www.google.com/search?q=" +encode+ "MP3" + "&ei=utf-8";
39}
Or else
41 {
The return "http://audio.search.yahoo.com/search/audio?&p=" +encode+ "&ei=utf-8";
43
44}
45}
//img
Or else if (document.getElementById ("Radiobuttonlist_kind_2"). Checked)
48 {
if (site== "Google")
50 {
"http://images.google.com/images?q=" +encode+ "&ei=utf-8";
52}
Or else
54 {
"Http://images.search.yahoo.com/search/images?p=" +encode+ "&ei=utf-8";
56}
57}
Or else
59 {
//alert ("err");
61}
62
63
64}
65
Button_google_onclick function ()
67 {
window.open (GETURL ("Google"));
69}
70
Button_yahoo_onclick function ()
72 {
window.open (GETURL ("Yahoo"));
74}
75
]]>//
</script>
<body>
<form id= "Form1" runat= "Server" >
Bayi <div>
<br/>
<br/>
<strong><span style= "FONT-SIZE:24PT; Color: #336633 ">search<br/>
</span></strong>
</div>
/> <BR
<table style= "left:0px; position:relative; top:0px ">
<tr>
<TD style= "WIDTH:31PX; height:21px ">
<span style= "Font-family:terminal" >Key</span></td>
<TD style= "WIDTH:253PX; height:21px ">
<input id= "Text_key" style= "WIDTH:248PX; Position:relative "type=" text "/></td>
<TD style= "WIDTH:175PX; height:21px ">
<asp:radiobuttonlist id= "Radiobuttonlist_kind" runat= "server" repeatdirection= "Horizontal"
Style= "position:relative" font-names= "terminal" >
<asp:listitem selected= "True" >Web</asp:ListItem>
<asp:ListItem>Mp3</asp:ListItem>
<asp:ListItem>Image</asp:ListItem>
</asp:RadioButtonList></td>
102 </tr>
<tr>
<TD style= "width:31px" >
</td>
<TD colspan= "2" >
<input id= "Button_google" style= "width:80px; Position:relative "type=" button "value=" Google "/>
108
109 <input id= "Button_yahoo" style= "left: -29px; width:104px; Position:relative "type=" button "
Value= "Yahoo!"/></td>
</tr>
112 </table>
113 <br/>
114 <asp:hyperlink id= "Hyperlink_home" runat= "Server" navigateurl= "~/default.aspx" style= "position:relative" &G T Home</asp:hyperlink></form>
116 </body>
117