JS Original eco-Ajax XMLHttpRequest imitation Baidu intelligent search function

Source: Internet
Author: User

HTML code

The code is as follows Copy Code

<style >
#apDiv1 {display:none; background: #FFF; border:1px solid #CCC; width:223px; padding:5px;}
</style>

<input type= "text" maxlength= "128" name= "title" id= "title" placeholder= "Enter Broker ID number" onkeyup= "getrenews (this.value);" autocomplete= "Off"/>

<ul id= "ApDiv1" style= "position:absolute;left:0px;top:24px" ></ul>

JS Ajax code is as follows

The code is as follows Copy Code

function Createxmlhttprequest () {//Create XMLHttpRequest Object
if (window. ActiveXObject) {//ie
try {
return new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {
Return
}
}else if (window. XMLHttpRequest) {//mozilla,firefox
try {
return new XMLHttpRequest ();
catch (e) {
Return
}
}
}

function Getrenews (Value) {//Keynote
var xmlhttp=createxmlhttprequest ();
var url = "ajaxsend.php?title=" +value+ "&mt=" +math.random (300000);
if (value== "") {
return false;
}

if (xmlHttp) {
callback = Getreadystatehandler (xmlHttp);
Xmlhttp.onreadystatechange = callback;
Xmlhttp.open ("Get", url,true);
Xmlhttp.send (NULL);
}
}

function Getreadystatehandler (xmlHttp) {//server returns post-processing functions
return function () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
if (Xmlhttp.responsetext) {
document.getElementById ("ApDiv1"). style.display= ' block ';
document.getElementById ("ApDiv1"). Innerhtml=xmlhttp.responsetext;
}else{
document.getElementById ("ApDiv1"). Innerhtml= "<li> Sorry, not found relative to the property!" </li> ";
}
}
}
}
}

The above code is in Baidu search to its own simple to modify a bit, because they used to feel the use of fast also did not use the jquery Ajax plug-ins.

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.