Ajax imitation Google suggest

Source: Internet
Author: User
 
 
  1. Javascript.js
  2. var URL = "Ajax.asp"   ; Background address
  3. var Time_delayajax = -   ; Search Latency
  4. var Time_delayupdown = -  ; Direction key delay
  5. Obj_div.style.top    = (xtop +) + "px"; 20 is almost the height of the input box, please adjust according to the actual situation
  6. ajax_xmlhttp.send ("sift_value=" +escape (temp_value));//Submit to backend value
  7. D D =d+ " < Li onmouseover= "Overli (" +i+ ");" onmousedown = "Downli (" +i+ ")" onmouseup = "Upli (" +i+ ", event)" onmousemove = "Moveli ();" > < span > about "+c[1]+" results </ span > "+c[0]+" </ Li > "; The display of//****li

The background output format must be ' Text 1, text 2 ' ...
' java,2 ' javascript,11 ' Java example, 22 ' etc

Default.css

 
  
  
  1. . ajaxsearch {
  2. width:300px;//hint layer widths

Home index.html

 
 
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. < HTML xmlns="http://www.w3.org/1999/xhtml">
  3. < Head >  
  4. < Meta http-equiv="content-type" Content=" text/html; Charset=utf-8 " />
  5. < link   rel = " stylesheet "   type = "text/css"   span class= "attribute" >href = "default.css"   />  
  6. < Script language="JavaScript" src="javascript.js" type = "Text/javascript" > </ Script>
  7. < title > Google suggest high Imitation example </ title >  
  8. </ Head >  
  9. < Body onResize="Removediv ();" >  
  10. < Div style="margin:20px 50px">
  11. < input   style = "width:298px;height:18px"   type = "text"   autocomplete = "off"   onblur = Blurdeal (); "   onkeydown =   onfocus =   />  
  12. </ Div >  
  13. </ Body >  
  14. </ HTML >  

Script Javascript.js

 
 
  1. ///////////////////////////////search Prompt box/////////////////////////////////  
  2. var     Obj_div; //Hint Layer object
  3. var     Obj_input; //Input Box object
  4. var     Main_delay; //Judgment Value change delay object
  5. var     Ajax_delay; //ajax Deferred Search object
  6. var    Updown_delay; //Direction key delay object
  7. var    ajax_xmlhttp; //ajax objects
  8. var div_word= NULL    ; //The current hint layer corresponding to the search value
  9. var     Li_num=-1; //pseudo cursor position, hint layer selected Li serial number, starting from 0
  10. var     Li_down=-1; //Mouse down the number of the cue layer
  11. var value_ed= ""    ; //The value of the input box before the delay
  12. var value_ing= ""    ; //The current value of the input box
  13. var value_unexit= ""   ; //search for a value with no results at the beginning
  14. var updown_run= false   ; //Allow the arrow keys up and down
  15. var ajax_run= false    ; //true for normal process, false stops Ajax
  16. var ajax_run_ing= false   ; //true is running, false is idle
  17. var input_focus= false   ; //text box focus
  18. var url= "Ajax.asp"    ; //Background address **********************************************************
  19. var   time_delayajax=300; //Search delay **********************************************************
  20. var  time_delayupdown=100; //direction key delay ********************************************************
  21. var  $= function (fun_id) {
  22. return document.getElementById (fun_id);
  23. }
  24. Try {  
  25. ajax_xmlhttp= New  ActiveXObject (' msxml2.xmlhttp ');
  26. Catch(e) {
  27. Try{
  28. ajax_xmlhttp= New  ActiveXObject (' microsoft.xmlhttp ');
  29. Catch(e) {
  30. Try{
  31. ajax_xmlhttp= New  XMLHttpRequest ();
  32. Catch(e) {ajax_xmlhttp=null;}
  33. }
  34. }
  35. ////////////////////////Create a hint layer////////////////////////  
  36. function Createajaxdiv () {
  37. var create_div = document.createelement ("div");
  38. Create_div.type = "Div"  ;
  39. var promptdiv = Document.body.appendChild (Create_div);
  40. Promptdiv.classname = "Ajaxsearch"  ;
  41. Obj_div=promptdiv;
  42. }
  43. ////////////////////////set the hint layer position////////////////////////  
  44. function Removediv () {
  45. if(!obj_div | |!obj_input) return false;
  46. if(obj_div.style.display=="None") return   false;
  47. var obj=obj_input;
  48. var xtop=0;
  49. var xleft=0;
  50. while(obj) {
  51. Xtop + + obj["offsettop"];
  52. Xleft + + obj["offsetleft"];
  53. obj = obj.offsetparent;
  54. }
  55. obj_div.style.left = xleft + "px"  ;
  56. Obj_div.style.top = (xtop +) + "px"    ;  //20 is almost the height of the input box, please adjust the ************************************************************8 according to the actual situation
  57. Li_down=-1;
  58. }
  59. ////////////////////////Hide hint layer////////////////////////  
  60. function Hideajaxdiv () {
  61. obj_div.style.display= "None"  ;
  62. Li_down=-1;
  63. }
  64. ////////////////////////Set the selected <li>css style////////////////////////  
  65. function Setlistyle () {
  66. for(var i=0;i<obj_div.firstchild.childnodes.length;i++) {
  67. obj_div.firstchild.childnodes[i].id= ""  ;
  68. }
  69. if(li_num!=-1) obj_div.firstchild.childnodes[li_num].id="liseleted"  ;
  70. }
  71. ////////////////////////mouse through the hint layer////////////////////////  
  72. function Overli (fun_seletedlinum) {
  73. if(li_num==-1) Value_ing=obj_input.value;
  74. Li_num=fun_seletedlinum;
  75. Setlistyle ();
  76. }
  77. ////////////////////////Mouse Drag hint layer////////////////////////  
  78. function Moveli () {
  79. if(window.getselection) {
  80. SetFocus ();
  81. Window.getselection (). Removeallranges ();
  82. }else{
  83. Document.selection.empty ();
  84. SetFocus ();
  85. }
  86. }
  87. ////////////////////////Mouse presses the hint layer////////////////////////  
  88. function Downli (fun_seletedlinum) {
  89. if(!obj_input) return false;
  90. Li_down=fun_seletedlinum;
  91. input_focus= true  ;
  92. }
  93. ////////////////////////mouse bounce cue layer////////////////////////  
  94. function Upli (fun_seletedlinum,fun_event) {
  95. if(!obj_input) return false;
  96. if(fun_event.button==2) {li_down=-1; return }  
  97. if(li_down!=fun_seletedlinum) {
  98. Li_down=-1;
  99. return false;
  100. }
  101. Cleartimeout (Ajax_delay);
  102. Cleartimeout (Updown_delay);
  103. updown_run= true  ;
  104. ajax_run= false  ;
  105. ajax_run_ing= false  ;
  106. Li_num=-1;
  107. div_word= NULL  ;
  108. Value_ed=obj_div.firstchild.childnodes[fun_seletedlinum].childnodes[1].nodevalue;
  109. obj_input.value=value_ed;
  110. value_ing=value_ed;
  111. Hideajaxdiv ();
  112. obj_div.innerhtml= ""  ;
  113. }
  114. ////////////////////////Set the text box to get focus///////////////////////  
  115. function SetFocus () {
  116. if(window.event) {
  117. var r = Obj_input.createtextrange ();
  118. R.movestart (' character ', obj_input.value.length);
  119. R.collapse (true);
  120. R.select ();
  121. }else{
  122. Obj_input.selectionstart=obj_input.value.length;
  123. Obj_input.focus ();
  124. }
  125. }
  126. ////////////////////////text box loses focus////////////////////////  
  127. function blurdeal () {
  128. if(input_focus==true) {
  129. SetFocus ();
  130. settimeout ("SetFocus ()");
  131. return false;
  132. }
  133. updown_run= false  ;
  134. ajax_run= false  ;
  135. ajax_run_ing= false  ;
  136. Clearinterval (Main_delay);
  137. Cleartimeout (Ajax_delay);
  138. Cleartimeout (Updown_delay);
  139. Hideajaxdiv ();
  140. if(value_ed!=obj_input.value) obj_div.innerhtml="";
  141. }
  142. ////////////////////////text box to get focus////////////////////////  
  143. function Focusdeal (fun_event) {
  144. if(!obj_div) createajaxdiv ();
  145. if(input_focus==true) {
  146. input_focus= false  ;
  147. return false;
  148. }
  149. var obj= (window.event)?  Fun_event.srcElement:Fun_event.target);
  150. if(obj.type!="text") return  False;
  151. updown_run= true  ;
  152. ajax_run= true  ;
  153. ajax_run_ing= false  ;
  154. if(obj_input==obj && value_ed==obj.value && obj_div.innerhtml!="" ){  
  155. obj_div.style.display= "Block"  ;
  156. Removediv ();
  157. }else{
  158. Obj_input=obj;
  159. Value_ed=obj.value;
  160. Value_ing=obj.value;
  161. value_unexit= ""  ;
  162. Li_num=-1;
  163. Li_down=-1;
  164. div_word= NULL  ;
  165. obj_div.innerhtml= ""  ;
  166. Removediv ();
  167. }
  168. main_delay=setinterval ("Mainajax ()", 10);
  169. }
  170. ////////////////////////main function////////////////////////  
  171. function Mainajax () {
  172. if(value_ed==obj_input.value) return false;
  173. if(value_unexit!="" && (obj_input.value). IndexOf (Value_unexit ==0) {hideajaxdiv (); obj_div.innerhtml=""; return   false;}
  174. if(value_ed!=obj_input.value && ajax_run_ing==false) {
  175. ajax_run= true  ;
  176. Value_ed=obj_input.value;
  177. Cleartimeout (Ajax_delay);
  178. if(obj_input.value!="") {
  179. ajax_delay=settimeout ("Getsearch ()";  , Time_delayajax);
  180. }else{
  181. Hideajaxdiv ();
  182. obj_div.innerhtml= ""  ;
  183. ajax_run= false  ;
  184. return false;
  185. }
  186. }
  187. }
  188. ////////////////////////Get search content////////////////////////  
  189. function Getsearch () {
  190. var Temp_value=obj_input.value;
  191. if(ajax_xmlhttp==null) {
  192. return false;
  193. Else if(ajax_xmlhttp.readystate!=0) {
  194. Ajax_xmlhttp.abort ();
  195. ajax_run_ing= false  ;
  196. }
  197. ajax_xmlhttp.onreadystatechange= function (){  
  198. if(ajax_run==false) {ajax_xmlhttp.abort (); ajax_run_ing=false ; return   false;}
  199. if(ajax_xmlhttp.readystate==4) {
  200. obj_div.innerhtml= ""  ;
  201. if(ajax_xmlhttp.status==200 | | ajax_xmlhttp.status==304) {
  202. var Contant=ajax_xmlhttp.responsetext;
  203. if(contant!="" && ajax_run==true) {
  204. Div_word=temp_value;
  205. Obj_div.innerhtml=resetcontant (contant);
  206. obj_div.style.display= "Block"  ;
  207. Removediv (); Removediv ();
  208. }else{
  209. Hideajaxdiv ();
  210. }
  211. updown_run= true  ;
  212. ajax_run_ing= false  ;
  213. Li_num=-1;
  214. if(contant=="") Value_unexit=temp_value;
  215. }
  216. }
  217. }
  218. Ajax_xmlhttp.open ("POST", URL,true);
  219. Ajax_xmlhttp.setrequestheader (' Content-type ',' application/  X-www-form-urlencoded ');
  220. ajax_run_ing= true  ;
  221. ajax_xmlhttp.send ("sift_value="+escape (temp_value)); //Submit to backend value *****************************************  
  222. }
  223. ////////////////////////content Reorganization///////////////////////  
  224. function resetcontant (fun_contant) {
  225. if(fun_contant==null | | |  fun_contant=="") return "";
  226. var a=fun_contant.substring (1,fun_contant.length-1);
  227. if(fun_contant==null | | |  fun_contant=="") return "";
  228. var b=a.split ("'");
  229. var c;
  230. var D;
  231. d= "<ul>"  ;
  232. for (var i in b) {
  233. c=b[i].split (",");
  234.   //***************************************************************  
  235. d=d+ "<li onmouseover=" Overli ("+i+"); "onmousedown=" Downli ("+i+") " Onmouseup= "Upli" ("+i+", event) "Onmousemove=" Moveli (); ><span> about "+c[1]+" Results </span> "+c[0]+" </li>  ;
  236.   //***************************************************************  
  237. }
  238. d=d+ " <li onmousedown=" Input_focus=true;li_down=-1 "onmouseup=" Li_down=-1 "onmousemove=" Moveli (); " ><span><a class= "Shutajaxdiv" onclick= "Hideajaxdiv ();" > Close </a></span></li> "
  239. d=d+ "</ul>"  ;
  240. return D;
  241. }
  242. ////////////////////////Keyboard Event////////////////////////  
  243. function Keydowndeal (fun_event) {
  244. var keyc= (window.event)?  Fun_event.keyCode:Fun_event.which);
  245. if(keyc==13) {hideajaxdiv (); return   false;}
  246. if(keyc==27) {
  247. if(obj_div.style.display=="block" && li_num>-1) value  _ed=obj_input.value=value_ing;
  248. Hideajaxdiv ();
  249. return false;
  250. }
  251. if(keyc==40 | | keyc==38) {
  252. if(div_word==obj_input.value && obj_div.style.display=="None" && obj_div.innerhtml!="") {
  253. obj_div.style.display= "Block"  ;
  254. Removediv ();
  255. return false;
  256. }
  257. if(li_num==-1) {
  258. if(div_word!=obj_input.value) return false;
  259. }else{
  260. if(div_word!=value_ing) return false;
  261. }
  262. if(updown_run==false | | ajax_run_ing==true | | obj_  div.style.display=="None") return false;
  263. updown_delay=settimeout ("Updownli" ("+keyc+") ", Time_delayupdown  );
  264. updown_run= false  ;
  265. }
  266. }
  267. ////////////////////////Direction key mobile li////////////////////////  
  268. function Updownli (fun_key) {
  269. if(!obj_div) { return false;}
  270. updown_run= true  ;
  271. if(li_num==-1) {
  272. if(div_word!=obj_input.value) {hideajaxdiv (); obj_div.innerhtml="" ; li_num=-1; return   false;}
  273. }else{
  274. if(div_word!=value_ing) {hideajaxdiv (); obj_div.innerhtml=""; li_num=-1; return   false;}
  275. }
  276. if(updown_run==false) return  False;
  277. if(li_num==-1) value_ing=value_ed;
  278. if(fun_key==40) {
  279. if(li_num<obj_div.firstchild.childnodes.length-2) {
  280. li_num++;
  281. }else{
  282. Li_num=-1;
  283. }
  284. }
  285. if(fun_key==38) {
  286. if(li_num>=0) {
  287. li_num--;
  288. }else{
  289. Li_num=obj_div.firstchild.childnodes.length-2;
  290. }
  291. }
  292. if(li_num!=-1) {
  293. Value_ed=obj_input.value=obj_div.firstchild.childnodes[li_num].childnodes[1].nodevalue;
  294. }else{
  295. value_ed=obj_input.value=value_ing;
  296. }
  297. Setlistyle ();
  298. }

Backstage ajax.asp

 
 
  1. <% @LANGUAGE = "VBSCRIPT" codepage= "65001" %>
  2. <%option explicit%>
  3. <%response.codepage= "65001" %>
  4. <%response.charset= "Utf-8" %>
  5. <!--#include file= "Conn.asp" -->
  6. <%
  7. Dim sift_value
  8. Dim sql,rs,i,num
  9. Dim contant
  10. contant= ""  
  11. num=10
  12. Sift_value=replace (unescape (Request.Form ("Sift_value")),"" ", """""")  
  13. sql= "SELECT Top" &Num& "Keyword,matchnum from search where keyword like" "" &Sift_value& "%" "ORDER by id"  
  14. set Rs=server. CreateObject ("Adodb.recordset")
  15. Rs.Open sql,conn,1,1
  16. If not (rs.eof and RS.BOF) then
  17. For I=0 to Num-1
  18. contant=contant& "'" &rs (0) & "," &rs (1) & "'"  
  19. Rs.movenext
  20. If Rs.eof then exit for
  21. Next
  22. End If
  23. Response. Write (contant)
  24. Rs.close
  25. Set rs=nothing
  26. %>

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.