JS implementation of smart prompts like google and Baidu search boxes

Source: Internet
Author: User

JS implementation of smart prompts like google and Baidu search boxes

This article mainly introduces how to implement the smart prompts of input information in the search box of google and Baidu using javascript. The example shows how to implement the smart prompt function using javascript, which is of great practical value, for more information, see

This article describes how to implement smart prompts in the search box of google and Baidu using JavaScript. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

<! 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>

<Title> implementation of smart prompts similar to input information in google and Baidu search boxes </title>

<Style type = "text/css" media = "screen">

Body

{

Font: 11px arial;

}

. Suggest_link

{

Width: 120px;

Background-color: # FFFFFF;

Padding: 2px 6px 2px 6px;

}

. Suggest_link_over

{

Width: 120px;

Background-color: # E8F2FE;

Padding: 2px 6px 2px 6px;

}

# SuggestResult

{

Position: absolute;

Background-color: # FFFFFF;

Text-align: left;

Border: 1px solid #000000;

}

/* Input */

. Input_on

{

Padding: 2px 8px 0pt 3px;

Height: 18px;

Border: 1px solid #999;

Background-color: # FFFFCC;

}

. Input_off

{

Padding: 2px 8px 0pt 3px;

Height: 18px;

Border: 1px solid # CCC;

Background-color: # FFF;

}

. Input_move

{

Padding: 2px 8px 0pt 3px;

Height: 18px;

Border: 1px solid #999;

Background-color: # FFFFCC;

}

. Input_out

{

/* Height: 16px; default height */

Padding: 2px 8px 0pt 3px;

Height: 18px;

Border: 1px solid # CCC;

Background-color: # FFF;

}

</Style>

<Script language = "javascript" type = "text/javascript">

Var $ = document. getElementById;

// Create an XMLHttpRequest object

Function createXMLHttpRequest (){

Var obj;

If (window. XMLHttpRequest) {// Mozilla Browser

Obj = new XMLHttpRequest ();

}

Else if (window. ActiveXObject) {// IE browser

Try {

Obj = new ActiveXObject ("Msxml2.XMLHTTP ");

} Catch (e ){

Try {

Obj = new ActiveXObject ("Microsoft. XMLHTTP ");

} Catch (e ){}

}

}

Return obj;

}

// This function is called when the content in the input box changes.

Function searchSuggest (){

Var inputField = $ ("txtSearch ");

Var suggestText = $ ("suggestResult ");

If (inputField. value. length> 0 ){

Var o = createXMLHttpRequest ();

Var url = "SearchResult. ashx? SearchText = "+ escape (inputField. value );

O. open ("GET", url, true );

O. onreadystatechange = function (){

If (o. readyState = 4 ){

If (o. status = 200 ){

Var sourceItems = o. responseText. split ("\ n ");

If (sourceItems. length> 1 ){

SuggestText. style. display = "";

SuggestText. innerHTML = "";

For (var I = 0; I <sourceItems. length-1; I ++ ){

Var sourceText = sourceItems [I]. split ("@") [1];

Var sourceValue = sourceItems [I]. split ("@") [0];

Var s = "<div onmouseover = \" javascript: suggestOver (this );\"";

S + = "onmouseout = \" javascript: suggestOut (this );\"";

S + = "onclick = \" javascript: setSearch ('"+ sourceText +"', '"+ sourceValue + "');\"";

S + = "class = \" suggest_link \ ">" + sourceText + "</div> ";

SuggestText. innerHTML + = s;

}

}

Else {

SuggestText. style. display = "none ";

}

}

}

}; // Specify the Response Function

O. send (null); // send the request

}

Else {

SuggestText. style. display = "none ";

}

}

Function delayExecute (){

$ ("ValueResult"). value = "";

Window. setTimeout (function () {searchSuggest ()}, 800 );

// Delay processing

}

Function suggestOver (div_value ){

Div_value.className = "suggest_link_over ";

}

Function suggestOut (div_value ){

Div_value.className = "suggest_link ";

}

Function setSearch (a, B ){

$ ("TxtSearch"). value =;

$ ("ValueResult"). value = B;

Var div = $ ("suggestResult ");

Div. innerHTML = "";

Div. style. display = "none ";

}

Function showResult (){

Alert ($ ("txtSearch"). value + $ ("valueResult"). value );

}

</Script>

</Head>

<Body>

<Form id = "form1" action = "">

<Input type = "text" id = "txtSearch" name = "txtSearch" onkeyup = "delayExecute ();" size = "20"

Class = "input_out" onfocus = "this. className = 'input _ on'; this. onmouseout = ''"

Onblur = "this. className = 'input _ off'; this. onmouseout = function () {this. className = 'input _ out '};"

Onmousemove = "this. className = 'input _ move '" onmouseout = "this. className = 'input _ out'"/>

<Input type = "hidden" id = "valueResult" name = "valueResult" value = ""/>

<Br/>

<Div id = "suggestResult" style = "display: none">

</Div>

<Br/>

<Input id = "button1" type = "button" value = "Submit" onclick = "showResult ();"/>

</Form>

</Body>

</Html>

Server C # code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<% @ WebHandler Language = "C #" Class = "SearchResult" %>

Using System;

Using System. Web;

Using System. Data;

Public class SearchResult: IHttpHandler {

Public void ProcessRequest (HttpContext context ){

Object QueryWord = context. Request. QueryString ["searchText"];

If (QueryWord! = Null)

{

If (QueryWord. ToString (). Trim (). Length> 0)

{

DataTable dt = getDB ();

String returnText = "";

If (dt! = Null & dt. Rows. Count> 0)

{

DataRow [] dr = dt. Select ("name like '%" + QueryWord. ToString () + "% '");

If (dr. Length> 0)

{

For (int I = 0; I <dr. Length; I ++)

{

// You can set to return multiple strings.

ReturnText + = dr [I] ["id"]. ToString () + "@" + dr [I] ["name"]. ToString () + "\ n ";

}

}

}

Context. Response. Write (returnText );

Context. Response. End ();

}

}

}

Public bool IsReusable {

Get {

Return false;

}

}

/// <Summary>

/// Method for obtaining the data source

/// </Summary>

/// <Returns> data source </returns>

Private DataTable getDB ()

{

DataTable dt = new DataTable ();

Dt. Columns. Add ("id ");

Dt. Columns. Add ("name ");

Dt. Columns. Add ("age ");

Dt. Rows. Add (new object [] {"000001", "Zhang San", "26 "});

Dt. Rows. Add (new object [] {"000002", "Zhang Xiao", "26 "});

Dt. Rows. Add (new object [] {"000003", "Zhang Lan", "27 "});

Dt. Rows. Add (new object [] {"000004", "Li Si", "25 "});

Dt. Rows. Add (new object [] {"000005", "Li Xing", "27 "});

Return dt;

}

}

The preceding section describes how to implement the smart prompts of input information in the search box similar to google or Baidu using JS. We hope this article will help you design javascript programs.

Related Article

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.