Ajax implementation of the Web page multitasking task _ Tasks

Source: Internet
Author: User

Today you need to write a function, for the data in the database, when the user is displayed through the Web, automatically start the server-side script to test the validity of each data, and the results presented to the user.

What I'm thinking about is that when you render each piece of data, you start an AJAX communication process and feedback the results back on OK.

The details need to be considered in terms of how each process completes its task and how it knows its "host" data (which data is the task) at the time of the callback.

And since XMLHttpRequest is private for every task, we need multitasking, so I use a class to encapsulate the entire task. Each time a class object is started, it is used as a private member of the class.

Here's a look at our client-side scripting model

AJAX Process class functions that detect URLs taskajaxprocess () {//class member XMLHttpRequest var http_request_check_url = false;/start Task This.start_ Task = function ($obj) {var checkurl = $obj. alt;////////////Create HTTP request//////////////////http_request_check_url=fal Se Starts initializing the XMLHttpRequest object if (window). XMLHttpRequest)//mozilla Browser {http_request_check_url=new XMLHttpRequest (); if (http_request_check_ Url.overridemimetype) {//Set MIME category Http_request_check_url.overridemimetype ("Text/xml");} else if (window. ActiveXObject)//ie Browser {try {http_request_check_url=new ActiveXObject ("Msxml2.xmlhttp");} catch (e) {try {http_request _check_url=new ActiveXObject ("Microsoft.XMLHTTP"); catch (e) {}}}//exception, create object instance failed if (!http_request_check_url) {document.getElementById ("SearchResult"). Innerhtml= "< Font Color=red>fault error: Failed to create XMLHTTP object. </font> "; return false; ///Add random number in URL to prevent browser cache page//Send initiator (task host) ID into PHP script var post_url= "checkurl.php?url=" + Checkurl + "&id=" + $obj. ID + "&A Mp;random= "+ math.random () * (10000); http_request_check_url.onreadystatechange=this.showcheckurlresult;//Registry-Completed callback function Http_request_check_url.open ("Get" , post_url,true); Http_request_check_url.send (NULL); }//Completed callback function This.showcheckurlresult = function () {if (http_request_check_url.readystate==4) {if (http_request_check_ URL.STATUS==200)//Information Returns success {var response = eval ("+ Http_request_check_url.responsetext +")); Processing the result here//sending the ID back, it is the task host class}}//function}//Start task, pass in HTML object function Start_task ($obj) {//start task var s = new Taskajaxproc ESS (); S.start_task ($obj); }

The server-side script is slightly, and is roughly followed by the ID of the task initiator that came with it.

You can implement multitasking at the same time and then process the callback in the interface.

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.

Tags Index: