Using XMLHTTP Ajax to automatically save information complete JS code

Source: Internet
Author: User
Tags ticket

Use XMLHTTP Ajax to automatically save information complete JS code
Function Createhttphandler () {
 var HttpHandler = false;
 
  @if (@_jscript_version >= 5)
  //JScript gives us conditional compilation, we can cope with old IE Versions. The
  //and security blocked creation of the objects.
  try {
   httphandler = new ActiveXObject ("Msxml2.xmlhttp");
  
  catch (e) {
   try {
    httphandler = new ActiveXObject (" Microsoft.XMLHTTP ");
   }
   catch (e) {
    httphandler = false;
   
  &NBSP}
 
 if (!httphandler && typeof xmlhttprequest!= ' undefined ') {
   HttpHandler = new XMLHttpRequest ();
 }
 return HttpHandler;
}

/**
* Auto saves as draft
*/
function Autosavedraft () {
Checks = 0;
seconds = Now ();

var title = encodeURI (Addform.title.value);
var BODY = encodeURI (addform.body.value);
var catid = Addform.catid.options[addform.catid.selectedindex].value;
var more = encodeURI (Addform.more.value);
var closed = 0;
if (addform.closed[0].checked) {
closed = Addform.closed[0].value;
}
else if (addform.closed[1].checked) {
closed = Addform.closed[1].value;
}
var ticket = Addform.ticket.value;

 var querystring = ' Action=autodraft ';
 querystring + = ' &title= ' + title;
 querystring + = ' &body= ' + body;
 querystring + = ' &catid= ' + catid;
 querystring + = ' &more= ' + more;
 querystring + = ' &closed= ' + closed;
 querystring + = ' &ticket= ' + ticket;
 if (FormType = = ' edit ') {
  querystring + = ' &itemid= ' + addform.itemid.value;
 &nbs P;querystring + = ' &type=edit ';
 }
 else {
  querystring + = ' &blogid= ' + addform.blogid.value;
  querystring = ' &type=add ';
 }
 if (Addform.draftid.value > 0) {
  querystring + = ' &draftid= ' + addform.draftid.value;< br>  }

Xmlhttprequest[0].open (' Post ', Goalurl, true);
Xmlhttprequest[0].onreadystatechange = Checkmonitor;
Xmlhttprequest[0].setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xmlhttprequest[0].send (querystring);

var querystring = ' action=updateticket&ticket= ' + ticket;

Xmlhttprequest[1].open (' Post ', Goalurl, true);
Xmlhttprequest[1].onreadystatechange = Updateticket;
Xmlhttprequest[1].setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xmlhttprequest[1].send (querystring);
}

/**
* Monitors the edits
*/
function Domonitor () {
if (Checks * (now ()-seconds) > 120 * 1000 * 50) {
Autosavedraft ();
}
else {
checks++;
}
}

/**
 * checks the process of the saving
 */
Function Checkmonitor () {
 if (xmlhttpreques T[0].readystate = = 4) {
  if (xmlhttprequest[0].responsetext) {
   if (XMLHttpRequest [0].responsetext.substr (0, 4) = = ' err: ') {
    goal.innerhtml = xmlhttprequest[0]. RESPONSETEXT.SUBSTR (4) + ' (' + formatteddate () + ') ';
   }
   else {
    addform.draftid.value = Xmlhttprequest[0].responsetext;
    goal.innerhtml = lastsavedtext + ' + formatteddate ();
   }
  }
 }
}

/**
* Checks the process of the ticket updating
*/
function Updateticket () {
if (xmlhttprequest[1].readystate = = 4) {
if (Xmlhttprequest[1].responsetext) {
if (xmlhttprequest[1].responsetext.substr (0, 4) = = ' err: ') {
goal.innerhtml = Xmlhttprequest[1].responsetext.substr (4) + ' (' + formatteddate () + ') ';
}
else {
Addform.ticket.value = Xmlhttprequest[1].responsetext;
}
}
}
}

/**
* Gets now in milliseconds
*/
function Now () {
var now = new Date ();
return Now.gettime ();
}

/**
 * gets now on the local dateformat
 */
Function formatteddate () {
 var now = new Da Te ();
 return now.tolocaledatestring () + ' + now.tolocaletimestring ();
}

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.