Create a search toolbar using javascript

Source: Internet
Author: User

I. Final Results

Ii. Principle

If you search for "China" in Yahoo, then the address bar in the browser will get a string of Address: 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 =
Look a little messy, simplify: http://search.cn.yahoo.com/search? & P = % D6 % D0 % B9 % FA
This is the key. & p = % D6 % D0 % B9 % FA is the keyword parameter for search, while % D6 % D0 % B9 % FA is "Chinese"
Url encoding. OK. We only need to construct such encoding.

Iii. URL Encoding
The encodeURIComponent () function of JavaScript can complete coding.
For example, the above example we can use "http://search.cn.yahoo.com/search? & P = "+ encodeURIComponent (" China.

4. Code
(Click the plus sign to expand)
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Search. aspx. cs" Inherits = "Search" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Search </title>
<Script language = "javascript" type = "text/javascript">
// <! CDATA [

Function GetEncodeOfKey ()
{
Var strKey = invalid argument Doc ument. getElementById ("Text_Key"). value;
Return encodeURIComponent (strKey );
}

Function GetUrl (site)
{
Var encode = GetEncodeOfKey ();

// Web
If (document. getElementById ("RadioButtonList_Kind_0"). checked)
{
If (site = "google ")
{
Return "http://www.google.com/search? Q = "+ encode +" & ei = UTF-8 ";
}
Else
{
Return "http://search.yahoo.com/search? P = "+ encode +" & ei = UTF-8 ";
}
}
// Mp3
Else if (document. getElementById ("RadioButtonList_Kind_1"). checked)
{
If (site = "google ")
{
Return "http://www.google.com/search? Q = "+ encode +" mp3 "+" & ei = UTF-8 ";
}
Else
{
Return "http://audio.search.yahoo.com/search/audio? & P = "+ encode +" & ei = UTF-8 ";

}
}
// Img
Else if (document. getElementById ("RadioButtonList_Kind_2"). checked)
{
If (site = "google ")
{
Return "http://images.google.com/images? Q = "+ encode +" & ei = UTF-8 ";
}
Else
{
Return "http://images.search.yahoo.com/search/images? P = "+ encode +" & ei = UTF-8 ";
}
}
Else
{
// Alert ("err ");
}


}

Function Button_Google_onclick ()
{
Window. open (GetUrl ("google "));
}

Function Button_Yahoo_onclick ()
{
Window. open (GetUrl ("yahoo "));
}

//]>
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Br/>
<Br/>
<Strong> <span style = "font-size: 24pt; color: #336633"> Search <br/>
</Span> </strong>
</Div>
<Hr style = "position: relative"/>
<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>
</Tr>
<Tr>
<Td style = "width: 31px">
</Td>
<Td colspan = "2">
<Input id = "Button_Google" style = "width: 80px; position: relative" type = "button" value = "Google" onclick = "return Button_Google_onclick ()"/>

<Input id = "Button_Yahoo" style = "left:-29px; width: pixel PX; position: relative" type = "button"
Value = "Yahoo! "Onclick =" return Button_Yahoo_onclick () "/> </td>
</Table>
<Br/>
<Hr style = "position: relative"/>
<Asp: HyperLink ID = "HyperLink_Home" runat = "server" NavigateUrl = "~ /Default. aspx "Style =" position: relative "> Home </asp: HyperLink> </form>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.