The addEvent function will continue to execute after handling the error using the _d function.- I am aware of this, this is done on purpose, because I check for the existence of a DOM object, but I prefer to do it in the main script, but you convinced me - I’ll add return false.
Pollution of the global variable space is a very bad tone.- here you are right, but except for readyList = []; I can not find anything global :)
// - 3 ( ) /** * Function insert DOM element before some element * * @version 2012-11-06 * @param Object new_element * @param Object targetElement * @return void */ function insertBefore(new_element, targetElement) { targetElement.parentNode.insertBefore(new_element, targetElement); } /** * Function insert DOM element after some element * * @version 2012-11-06 * @param Object new_element * @param Object targetElement * @return void */ function insertAfter(new_element, targetElement) { var parent = targetElement.parentNode; //if the parents lastchild is the targetElement... if(parent.lastchild == targetElement) parent.appendChild(new_element); else parent.insertBefore(new_element, targetElement.nextSibling); } /** * Function make clone of income object * * @version 2012-11-07 * @param Object obj * @return Object */ function clone(obj) { if(obj == null || typeof(obj) != 'object') return obj; var temp = {}; for(var key in obj) temp[key] = clone(obj[key]); return temp; } // DOM , /** * Function return next element in dom object * * @version 2013-01-08 * @param Object obj * @return Object */ function getNext(obj) { obj = obj.nextSibling; if(!obj) return false; while(obj.nodeType != 1){ obj = obj.nextSibling; if(!obj) return false; } return obj; } // DOM , /** * Function return previous element in dom object * * @version 2013-02-13 * @param Object obj * @return Object */ function getPrevious(obj) { obj = obj.previousSibling; if(!obj) return false; while(obj.nodeType != 1){ obj = obj.previousSibling; if(!obj) return false; } return obj; } // 2 - Cookies. C , , /** * Function new Cookies * * @version 2013-03-26 * @param string name * @param string value * @param string expires * @param string path * @return void */ function setCookies(name, value, expires, path) { if(!name || !value) return 0; if(!expires){ expires = new Date(); expires.setTime(expires.getTime() + (1000 * 86400 * 365)); //save 1 year } if(!path) path = '/'; // set Cookies document.cookie = name+'='+escape(value)+'; expires='+expires.toGMTString()+'; path='+path; } /** * Function return Cookies value by name * * @version 2013-03-26 * @param string name * @return void */ function getCookies(name) { var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' ); if ( results ) return ( unescape ( results[2] ) ); else return null; } // // Ajax(params), prototype /** * Ajax object * * Send request by post/get to some url * * @version 2013-02-13 * @param Object params * @return Object */ window.Ajax = function(params) { // this.options = { // default url url: '', // default method method: 'get', // Is synchronous request? async: true, // in seconds timeout: 10000, // callback function, in default - empty function onComplete: function(){} }; // set config params this.setConfig(params); // XMLHttpRequest // initialize this.init(params); }; /** * Pablic methods */ window.Ajax.prototype = { /** * some internal params */ xml_http_request: null, timeout: null, // json|xml|text response:'json', // ajax , /** * configure functionality */ setConfig: function(opt) { // set url if(opt.url != undefined) this.options.url = opt.url; // set method if(opt.method != undefined) this.options.method = opt.method; // set asynchronus param if(opt.async != undefined) this.options.async = opt.async; // set timeout if(opt.timeout != undefined) this.options.timeout = opt.timeout; // set callback functions if((opt.onComplete != undefined) && (typeof(opt.onComplete) == 'function')) this.options.onComplete = opt.onComplete; }, /** * Initialize XMLHTTPRequest */ init: function() { // XMLHttpRequest // Cross-browser compatibility for browsers if (typeof XMLHttpRequest != 'undefined') { this.xml_http_request = new XMLHttpRequest(); } else{ try { this.xml_http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { this.xml_http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { alert('Your browser don\'t support Ajax technology. Please download real browser :)'); } } } var self_ = this; // (post/get), url, / // open XMLHttpRequest this.xml_http_request.open(self_.options.method, self_.options.url, self_.options.async); /** callback * * readyState : * 0 - Unitialized * 1 - Loading * 2 - Loaded * 3 - Interactive * 4 - Complete * 0-2 . * * self_.xml_http_request.status - (200, 404 ) */ // set callback function for XMLHttpRequest this.xml_http_request.onreadystatechange = function(){ if((self_.xml_http_request.readyState == 4) && (self_.xml_http_request.status > 0)){ // Ajax ( ) // delete timeout clearTimeout(self_.timeout); // , // JSON.parse - JSON ( - bye-bye) if(self_.response == 'json') var response = JSON.parse(self_.xml_http_request.responseText); else{ if(self_.response == 'xml') var response = self_.xml_http_request.responseXML; else var response = self_.xml_http_request.responseText; } // self_.options.onComplete(response); } } }, // , /** * Set some headers if need */ setRequestHeader: function(name, value) { this.xml_http_request.setRequestHeader(name, value); }, // Ajax /** * Send request */ send: function(params) { this.xml_http_request.send(params); var self_ = this; // , - // - 10 , // set timeout need for abort request this.timeout = setTimeout( function(){ self_.xml_http_request.abort(); }, this.options.timeout); }, // , , ( :) ) /** * Abort request */ abort: function() { this.xml_http_request.abort(); } } Ajax(): new Ajax({ url: '/index.php', timeout: 20000, method: 'post', onComplete: function(data){ alert(data);} }).send('qwerty=123');
Source: https://habr.com/ru/post/176273/
All Articles