AJax implements functions similar to Baidu search bar

韦小宝
Release: 2017-12-30 20:03:40
Original
1410 people have browsed it

The following is the function of ajax to implement a simple Baidu search bar. When the user types characters in the input box above, the function "showHint()" will be executed. The following is a very detailed introduction to you through the ajax example code. Friends who are interested in ajax should take a look.

During the internship process, the asynchronous submission function needs to be used, so I tried to understand ajax and instantly felt like I had done it before Learning is really a drop in the bucket. It is true. If you don’t know jquery and ajax when doing web application development, it is really useless. After understanding it, I feel that all the fancy functions on the Internet can be realized.

Without further ado, here is the ajax function to implement a simple Baidu search bar. When the user types characters in the input box above, the function "showHint()" will be executed. This function is triggered by the "onkeyup" event:


function showHint(str) { var xmlhttp; if (str.length==0) { document.getElementByIdx_x("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementByIdx_x("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gethint.asp?q="+str,true); xmlhttp.send(); }
Copy after login


Source code explanation:

If the input box is empty (str.length==0), the function clears the content of the txtHint placeholder and exits the function.

If the input box is not empty, the showHint() function performs the following tasks:

Creates an XMLHttpRequest object

Execute the function when the server response is ready

Request a file sent to the server

Please note that we added a parameter q (with the content of the input box) to the URL

The requested file is written in php, of course what to use All are OK, and there is no essential difference from ordinary pages:


 0) { $hint=""; for($i=0; $i
Copy after login


I heard that similar questions were asked when I went to Baidu for an interview. In fact, after understanding it, it is nothing more than that.

The above is the AJax implementation introduced by the editor to implement functions similar to the Baidu search bar (commonly seen in interviews). I hope it will be helpful to everyone! !

Related recommendations:

Analysis of reasons why ajax obtains json data as undefined_AJAX related

jquery implementation of ajaxfileupload asynchronous upload plug-in tutorial

Detailed explanation of ajax and same-origin policy implemented by JS

The above is the detailed content of AJax implements functions similar to Baidu search bar. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!