利用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();
}