Latest: AjaMyCore.js source listing /*** ** AjaMyAjax --> CORE AJAX functions library ** ** This software provided "AS IS," but is FREE to use and share. ** For details see the as-is doc and Creative Commons license: ** http://creativecommons.org/licenses/by-sa/3.0/ ** ** See doc.txt or examples.html for syntax and more info. ** http://AjaMyAjax.com (c) AjaMyAjax.com ** */ /** ** constructor and public variables: */ var pbcheckBrowserScript = true; var pbshowAlertMsgs = true; var pbshowStartUpIcon = true; var pbCoreLibReady = false; var pbXMLLibReady = false; var pbXSLLibReady = false; var pbJSONLibReady = false; var pbChartLibReady = false; var pbMyLibReady = false; var pbstateComplete = 4; var pbstatusOk = 200; var pbstatusNotFound = 404; var pbCoreLibHeader = "[AjaMyAjax Core Lib]"; var $ = function(strurl, strcontainer, strprocessType, stroutputFormat, strSearchElement, strSTSValue, strSTSKey, strPercentage) { // arguments: your file (url), the div or span tag, text or xml or json file type, and custom options var myAjax = new AjaMyAjax(); myAjax.ajaxRequest(strurl, strcontainer, strprocessType, stroutputFormat, strSearchElement, strSTSValue, strSTSKey, strPercentage); myAjax = null; } var AjaMyAjax = function() { /** ** xhr callback method: */ this.processResponse = function(xhrResp) { var strurl = (arguments[1]) ? arguments[1] : null; var strcontainer = (arguments[2]) ? arguments[2] : null; var strprocessType = (arguments[3]) ? arguments[3] : null; var stroutputFormat = (arguments[4]) ? arguments[4] : null; var strSearchElement = (arguments[5]) ? arguments[5] : null; var strSTSValue = (arguments[6]) ? arguments[6] : null; var strSTSKey = (arguments[7]) ? arguments[7] : null; var strPercentage = (arguments[8]) ? arguments[8] : null; if (!xhrResp || xhrResp.readyState < pbstateComplete) { return; } try { if (xhrResp.readyState == pbstateComplete) { if (xhrResp.status == pbstatusOk) { if (strcontainer && strprocessType) { if (strSTSValue) { // AjaMyAjax Simple Text Security (STS) // custom file checksum routine to validate // your document or data file's authenticity if (!isNaN(strSTSValue)) { var securityVerified = false; var docKey = 0, docText = ""; if (strprocessType.substr(0,1) === "X") { // xml, xsl response var nodeText = xhrResp.responseXML.getElementsByTagName('*').item(0); docText = aja_getXmlNodeText(nodeText); docKey = aja_sumAlphaNumChars(docText); if (docKey == strSTSValue) { securityVerified = true; } nodeText = null; } else { // all other text reponses docText = xhrResp.responseText; docKey = aja_sumAlphaNumChars(docText); if (docKey == strSTSValue) { securityVerified = true; } } docText = null; // (you will need an initial STS Value to get here..) // un-comment this line to get your doc's initial key value: //alert(strurl + " " + docKey); // or this of the above value in a simple encoded form: //alert(strurl + " " + aja_encodeNum(docKey)); // or this with your additional encoding key: //alert(strurl + " " + aja_encodeNum(docKey, strSTSKey)); if (!securityVerified) { // optional check #2: test for an encoded key match if (strSTSKey) { if (aja_encodeNum(docKey, strSTSKey) == strSTSValue) { securityVerified = true; } } else { if (aja_encodeNum(docKey) == strSTSValue) { securityVerified = true; } } if (!securityVerified) { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Security check failed for " + strurl); } if (xhrResp) { xhrResp = null; } return; } } } } // XML, XSLT, JSON processing; otherwise standard output switch (strprocessType) { case "XML": if (pbXMLLibReady) { if (stroutputFormat === "CHART") { if (pbChartLibReady) { processChart(xhrResp.responseXML, strcontainer, stroutputFormat, strSearchElement, "XML", strPercentage); } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in Chart Lib load"); } } } else { processXML(xhrResp.responseXML, strurl, strcontainer, stroutputFormat, strSearchElement); } } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in XML Lib load"); } } break; case "XSL": if (pbXSLLibReady) { processXSL(xhrResp.responseXML, strurl, strcontainer, stroutputFormat); } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in XSL Lib load"); } } break; case "JSON": if (pbJSONLibReady) { if (stroutputFormat === "CHART") { if (pbChartLibReady) { processChart(xhrResp.responseText, strcontainer, stroutputFormat, strSearchElement, "JSON", strPercentage); } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in Chart Lib load"); } } } else { processJSON(xhrResp.responseText, strcontainer, strSearchElement, stroutputFormat); } } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in JSON Lib load"); } } break; case "MYLIB": if (pbMyLibReady) { processMyFunctions(xhrResp.responseXML, xhrResp.responseText, strurl, strcontainer, stroutputFormat, strSearchElement, strSTSValue, strSTSKey); } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Error in Custom Lib load"); } } break; default: aja_fill(strcontainer, xhrResp.responseText); } } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Data destination or output unspecified for " + strurl); } } } else if (xhrResp.status == pbstatusNotFound) { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, strurl + " not found for data request"); } } else { xhrResp.abort(); aja_empty(strcontainer); if (pbshowAlertMsgs) { // provide user with a generic alert msg // for a "not found" from a PHP response if (strprocessType === "PHP") { aja_alertMsg(pbCoreLibHeader, "No data found for this selection"); } else { aja_alertMsg(pbCoreLibHeader, "Sorry, cannot retrieve data requested for " + strurl); } } } } } catch (e) { xhrResp.abort(); if (strcontainer) { aja_empty(strcontainer); } if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Sorry, an unexpected error occurred during processing of " + strurl); } if (e.description) { if (pbshowAlertMsgs) { aja_alertMsg("Gen Error", e.description); } } } // clean up after: try { if (strurl) strurl = null; if (strcontainer ) strcontainer = null; if (strprocessType) strprocessType = null; if (stroutputFormat) stroutputFormat = null; if (strSearchElement) strSearchElement = null; if (strSTSValue) strSTSValue = null; if (strSTSKey) strSTSKey = null; } finally { xhrResp = null; } } // end of processResponse /** ** public utility functions: */ aja_fill = function(strcontainer, strtext) { if (strtext) { if (strcontainer !== "none") { document.getElementById(strcontainer).innerHTML = strtext; } } } aja_empty = function(strcontainer) { if (strcontainer !== "none") { document.getElementById(strcontainer).innerHTML = ""; } } aja_cloak = function(strcontainer) { // called first at times, so check container if (strcontainer) { document.getElementById(strcontainer).style.visibility = "hidden"; } } aja_reveal = function(strcontainer) { if (strcontainer) { document.getElementById(strcontainer).style.visibility = "visible"; } } aja_alertMsg = function() { var strtitle = (arguments[0]) ? arguments[0] : "Unknown Error"; var strmsg = (arguments[1]) ? arguments[1] : "program id #501"; alert(strtitle + "\r\n" + strmsg); } // AjaMyAjax Simple Text Security (STS) // sums alphanumerics only for speed aja_sumAlphaNumChars = function(txt) { var regexp = /[a-zA-Z0-9]/; var charsum = 0; var txtlen = txt.length; // sum unicode values found in text: for (var i=0; i < txtlen; i++) { if (regexp.test(txt.charAt(i))) { charsum += txt.charCodeAt(i); } } return charsum; } // simple encoding of a numeric key // as an additional security measure // (uses your key or Math.PI by default) aja_encodeNum = function(num, yourvalue) { if (num) { if (isNaN(num)) { return -1; } if (yourvalue) { if (isNaN(yourvalue)) { return -1; } num = Math.round(num * yourvalue); } else { num = Math.round(num * Math.PI); } } return num; } aja_getXmlNodeText = function(xmlnode) { try { // Firefox, etc. if (xmlnode.textContent) { return xmlnode.textContent; } // MS Internet Explorer else { return xmlnode.text; } } catch (e) { return null; } } /** ** MAIN user request method: */ this.ajaxRequest = function() { if (pbcheckBrowserScript) { // basic browser/javascript compatibility checks if (!document.getElementById) { if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "JavaScript error, browser upgrade or fix needed to run this"); } return; } try { var arrtest = []; arrtest.push("*"); arrtest = null; } catch (e) { if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "JavaScript error, browser upgrade probably needed"); } if (e.description) { if (pbshowAlertMsgs) { aja_alertMsg("Gen Error", e.description); } } return; } } var async = true; // asynchronous mode (default) var strurl = (arguments[0]) ? arguments[0] : null; var strparam = (arguments[1]) ? arguments[1] : null; // property for user's div, span, etc. var strcontainer = (arguments[2]) ? arguments[2] : null; // user option to process as XML, XSL, JSON, etc. var strprocessType = (arguments[3]) ? arguments[3] : null; // output formatting option: tables, etc. var stroutputFormat = (arguments[4]) ? arguments[4] : null; // optional XML start node var strSearchElement = (arguments[5]) ? arguments[5] : null; // optional Simple Text Security value var strSTSValue = (arguments[6]) ? arguments[6] : null; // optional Simple Text Security key var strSTSKey = (arguments[7]) ? arguments[7] : null; // optional chart value percentage to display var strPercentage = (arguments[8]) ? arguments[8] : null; // optional override for selected ajax call to show alerts pbshowAlertMsgs = (arguments[9] != null) ? arguments[9] : pbshowAlertMsgs; // optional override for show startup icon variable pbshowStartUpIcon = (arguments[10] != null) ? arguments[10] : pbshowStartUpIcon; // optional override for asynchronous mode async = (arguments[11] != null) ? arguments[11] : async; if (!strurl || !strcontainer) { if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Required parameters are missing"); } return; } if (strprocessType) { strprocessType = strprocessType.toUpperCase(); } if (stroutputFormat) { stroutputFormat = stroutputFormat.toUpperCase(); } if (strurl) { strurl = strurl.toLowerCase(); } if (strparam) { // add any additional parameters strurl += strparam; } // note this will also trap any invalid container names: try { // simple, fast 'loading' icon or indicator... if (pbshowStartUpIcon) { aja_fill(strcontainer, "*"); } else { aja_empty(strcontainer); } } catch (e) { if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Invalid container, check parameter list for " + strurl); } if (e.description) { if (pbshowAlertMsgs) { aja_alertMsg("Gen Error", e.description); } } return; } // change: run php xsl translation for these browsers instead of using javascript: if (strurl.indexOf(".xml") != -1 && strprocessType === "XSL") { if (navigator.userAgent.indexOf("Chrome") != -1 || navigator.userAgent.indexOf("Safari") != -1 || navigator.appVersion.indexOf("MSIE 9") != -1) { strurl = "phpxsl.php?url=" + strurl.replace(".xml", ".xsl"); strprocessType = "PHP"; } } pbCoreLibReady = true; var xhrRequest = null; var callmethod = "GET"; if (window.XMLHttpRequest) { // Firefox, Opera, Safari, etc. xhrRequest = new XMLHttpRequest(); // here is this code if you need it // but you will probably get spurious // error messages in debugging consoles // if (xhrRequest.overrideMimeType) { // xhrRequest.overrideMimeType('text/xml; charset=iso-8859-9'); // } } else if (window.ActiveXObject) { // MS Internet Explorer... // synchronous mode needed for older IE browsers // (notably IE6), to help with memory leaks... // runs a bit slower but there seems to be // less problems with IE6 in synch mode: async = false; xhrRequest = new ActiveXObject("Msxml2.XMLHTTP"); if (!xhrRequest) { xhrRequest = new ActiveXObject("Microsoft.XMLHTTP"); } // if (this.processResponse.overrideMimeType) { // this.processResponse.overrideMimeType('text/xml; charset=iso-8859-9'); // } } if (!xhrRequest) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbCoreLibHeader, "Sorry, browser does not support XMLHttpRequest"); } return; } xhrRequest.open(callmethod, strurl, async); // xhrRequest.setRequestHeader('content-type','text/xml'); if (window.XMLHttpRequest) { xhrRequest.source = this; xhrRequest.onreadystatechange = function() { xhrRequest.source.processResponse(xhrRequest, strurl, strcontainer, strprocessType, stroutputFormat, strSearchElement, strSTSValue, strSTSKey, strPercentage); }; } else { // MS Internet Explorer xhrRequest.onreadystatechange = function() { AjaMyAjax.apply(this); this.processResponse(xhrRequest, strurl, strcontainer, strprocessType, stroutputFormat, strSearchElement, strSTSValue, strSTSKey, strPercentage); }; } if (xhrRequest) { xhrRequest.send(null); } if (!async && navigator.userAgent.indexOf("Firefox") != -1) { // separate callback method needed for Firefox (3.xx and earlier), synchronous mode only this.processResponse(xhrRequest, strurl, strcontainer, strprocessType, stroutputFormat, strSearchElement, strSTSValue, strSTSKey, strPercentage); } } // end of ajaxRequest } // eop