利用XMLHTTP ajax自動儲存資訊完整js代碼

來源:互聯網
上載者:User

利用xmlhttp ajax自動儲存資訊完整js代碼
function createhttphandler() {
 var httphandler = false;
 
 @if (@_jscript_version >= 5)
  // jscript gives us conditional compilation, we can cope with old ie versions.
  // 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;
   }
  }
 
 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;
  querystring += '&type=edit';
 }
 else {
  querystring += '&blogid=' + addform.blogid.value;
  querystring += '&type=add';
 }
 if (addform.draftid.value > 0) {
  querystring += '&draftid=' + addform.draftid.value;
 }

 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 (xmlhttprequest[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 in the local dateformat
 */
function formatteddate() {
 var now = new date();
 return now.tolocaledatestring() + ' ' + now.tolocaletimestring();
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.