plain XMLHttpRequest

This commit is contained in:
Alexander Yakovlev 2018-12-08 01:47:46 +07:00
parent a9b3716649
commit e83b88fe14
2 changed files with 42 additions and 32 deletions

View file

@ -1,11 +1,3 @@
/*!
* najaxjs micro ver 1.1.0 - najax-micro.js
* (c) any-js - https://github.com/any-js/najaxjs/
* Released under the MIT license
*/
var $najax={};!function(a){"use strict";function b(){var a=null;try{a=new XMLHttpRequest}catch(b){try{a=new ActiveXObject("Msxml2.XMLHTTP")}catch(c){try{a=new ActiveXObject("Microsoft.XMLHTTP")}catch(d){error("can't create xhr object.")}}}return a}function c(a,b,c,d){a._xhr.onreadystatechange=function(){var e=a._xhr;if(4==e.readyState){var f=e.response||"json"!=e.responseType&&e.responseText;200==e.status?b&&b.call(a,f,e):c&&c.call(a,e.status,e),d&&d.call(a,f,e)}}}function d(a){var b=a._xhr;if(a._open(),a._type&&(b.responseType=a._type),a._begin&&a._begin(b),null!==a._post){b.setRequestHeader("Content-Type","application/x-www-form-urlencoded");var c="",d=a._post;if("object"==typeof d){for(var e in d)d.hasOwnProperty(e)&&(c+=encodeURIComponent(e)+"="+encodeURIComponent(d[e])+"&");c=c.slice(0,-1)}else c=d;b.send(c)}else b.send()}var e=function(a,c){this._url=a,this._post=null,this._type=c||"json",this._begin=null,this._xhr=null,this._open=function(){this._xhr.open(null!==this._post?"POST":"GET",this._url,!0)},this._xhr=b()};e.prototype.begin=function(a){return this._begin=a,this},e.prototype.post=function(a){return this._post=a||"",this},e.prototype.done=function(a,b,e){return c(this,a,b,e),d(this),this},e.prototype.xhr=function(){return this._xhr},a.request=function(a,b){return new e(a,b)}}($najax);
/* Event listener */
function addEvtListener (obj, evtName, f) {
if (document.addEventListener) {

View file

@ -121,37 +121,55 @@ function catchClickHeat (e) {
showClickHeatDebug('Ready to send click data...');
/* Local request (not starting with "http")? Try an ajax call */
if (clickHeatServer.indexOf('http') !== 0) {
try {
$najax.request(clickHeatServer).post(params).done(function(data) {
if (data.status === 200) {
showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br/>x = ' + x + ' (' + (x - scrollx) + 'px from left + ' + scrollx + 'px of horizontal scrolling, max width = ' + winw + ')<br/>y = ' + y + ' (' + (y - scrolly) + 'px from top + ' + scrolly + 'px of vertical scrolling, max height = ' + winh + ')<br/>width = ' + w + '<br/>browser = ' + clickHeatBrowser + '<br/>click = ' + c + '<br/>site = ' + clickHeatSite + '<br/>group = ' + clickHeatGroup + '<br/><br/>Server answer: ' + xmlhttp.responseText);
} else if (xmlhttp.status === 404) {
showClickHeatDebug('click.php was not found at: ' + (clickHeatServer !== '' ? clickHeatServer : '/clickheat/click.php') + ' please set clickHeatServer value');
} else {
showClickHeatDebug('click.php returned a status code ' + xmlhttp.status + ' with the following error: ' + xmlhttp.responseText);
}
/* Stop waiting */
clickHeatLocalWait = 0;
});
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (er) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
xmlhttp = null;
}
}
if (!xmlhttp && typeof(XMLHttpRequest) !== 'undefined') {
xmlhttp = new XMLHttpRequest();
}
if (xmlhttp) {
if (clickHeatDebug === true) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br/>x = ' + x + ' (' + (x - scrollx) + 'px from left + ' + scrollx + 'px of horizontal scrolling, max width = ' + winw + ')<br/>y = ' + y + ' (' + (y - scrolly) + 'px from top + ' + scrolly + 'px of vertical scrolling, max height = ' + winh + ')<br/>width = ' + w + '<br/>browser = ' + clickHeatBrowser + '<br/>click = ' + c + '<br/>site = ' + clickHeatSite + '<br/>group = ' + clickHeatGroup + '<br/><br/>Server answer: ' + xmlhttp.responseText);
}
else if (xmlhttp.status === 404) {
showClickHeatDebug('click.php was not found at: ' + (clickHeatServer !== '' ? clickHeatServer : '/clickheat/click.php') + ' please set clickHeatServer value');
} else {
showClickHeatDebug('click.php returned a status code ' + xmlhttp.status + ' with the following error: ' + xmlhttp.responseText);
}
/* Stop waiting */
clickHeatLocalWait = 0;
}
};
}
xmlhttp.open('GET', clickHeatServer, true);
xmlhttp.send(JSON.serialize(params));
sent = true;
} catch (er) {
}
}
if (sent === false) {
/* This test is needed, as it includes the call to click.php in the iframe */
if (clickHeatDebug === true) {
showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br/>x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br/>y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br/>width = ' + w + '<br/>browser = ' + clickHeatBrowser + '<br/>click = ' + c + '<br/>site = ' + clickHeatSite + '<br/>group = ' + clickHeatGroup + '<br/><br/>Server answer:<br/>' + '<iframe src="' + clickHeatServer + '?' + params + '" width="700" height="60"></iframe>');
} else {
if (sent === false) {
/* This test is needed, as it includes the call to click.php in the iframe */
if (clickHeatDebug === true) {
showClickHeatDebug('Click recorded at ' + clickHeatServer + ' with the following parameters:<br/>x = ' + (x + scrollx) + ' (' + x + 'px from left + ' + scrollx + 'px of horizontal scrolling)<br/>y = ' + (y + scrolly) + ' (' + y + 'px from top + ' + scrolly + 'px of vertical scrolling)<br/>width = ' + w + '<br/>browser = ' + clickHeatBrowser + '<br/>click = ' + c + '<br/>site = ' + clickHeatSite + '<br/>group = ' + clickHeatGroup + '<br/><br/>Server answer:<br/>' + '<iframe src="' + clickHeatServer + '?' + params + '" width="700" height="60"></iframe>');
} else {
clickHeatImg = new Image();
clickHeatImg.src = clickHeatServer + '?' + serializeObjectToQuery(params);
// clickHeatImg.onload = function() { clickHeatLocalWait = 0; }
}
}
}
/* Little waiting cycle: default is to wait until Ajax sent or until the end of the time if no Ajax is available */
now = new Date();
clickHeatLocalWait = now.getTime() + clickHeatWait;
while (clickHeatLocalWait > now.getTime()) {
/* Little waiting cycle: default is to wait until Ajax sent or until the end of the time if no Ajax is available */
now = new Date();
clickHeatLocalWait = now.getTime() + clickHeatWait;
while (clickHeatLocalWait > now.getTime()) {
now = new Date();
}
}
} catch (err) {
showClickHeatDebug('An error occurred while processing click (Javascript error): ' + err.message);