Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Mel Smith" Newsgroups: comp.lang.javascript Subject: Re: Puzzling JS Error Date: Thu, 9 Apr 2015 16:26:11 -0600 Lines: 123 Message-ID: References: X-Trace: individual.net CY4VaAAPgmgifxBykJPDWQmJNm5+I9xExr0QSAKzMs40IEJAjP Cancel-Lock: sha1:mG95eYY6NdkTVH+i/pXRzQ3HT7w= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-RFC2646: Format=Flowed; Original Xref: csiph.com comp.lang.javascript:27285 Hi Evertjan: Here is the complete Ajax set up from an *older working* Ajax initializer that still works. Why does this *still* work when my newer one fails ? Thanks for a bit more insight into that 'throw' statement (and there is no visible message when the newer one 'fails' (I'm really mixed up now) -Mel Smith : ****** start of 'Older' Working Ajax initializer ******** // Make sure we haven't already been loaded var HTTP; if (HTTP && (typeof HTTP != "object" || HTTP.NAME)) { throw new Error("Namespace 'HTTP' already exists"); } // Create our namespace, and specify some meta-information HTTP = {}; HTTP.NAME = "HTTP"; // The name of this namespace HTTP.VERSION = 1.0; // The version of this namespace // This is a list of XMLHttpRequest creation factory functions to try HTTP._factories = [ function() { return new XMLHttpRequest(); }, function() { return new ActiveXObject("Msxml2.XMLHTTP"); }, function() { return new ActiveXObject("Microsoft.XMLHTTP"); } ]; // When we find a factory that works, store it here HTTP._factory = null; /** * Create and return a new XMLHttpRequest object. * * The first time we're called, try the list of factory functions until * we find one that returns a nonnull value and does not throw an * exception. Once we find a working factory, remember it for later use. */ HTTP.newRequest = function() { if (HTTP._factory != null) return HTTP._factory(); for(var i = 0; i < HTTP._factories.length; i++) { try { var factory = HTTP._factories[i]; var request = factory(); if (request != null) { HTTP._factory = factory; return request; } } catch(e) { continue; } } // If we get here, none of the factory candidates succeeded, // so throw an exception now and for all future calls. HTTP._factory = function() { throw new Error("XMLHttpRequest not supported"); } HTTP._factory(); // Throw an error } HTTP.post = function(url, values, callback) { var request = HTTP.newRequest(); request.open("POST", url); // This header tells the server how to interpret the body of the request request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 200 ) { callback(request.responseText); } else { callback(request.status); } } } // Encode the properties of the values object and send them as // the body of the request. request.send(HTTP.encodeFormData(values)); scrrequest=request; // set global vrbl screquest }; /** * Encode the property name/value pairs of an object as if they were from * an HTML form, using application/x-www-form-urlencoded format */ HTTP.encodeFormData = function(data) { var pairs = []; var regexp = /%20/g; // A regular expression to match an encoded space for(var name in data) { var value = data[name].toString(); // Create a name/value pair, but encode name and value first // The global function encodeURIComponent does almost what we want, // but it encodes spaces as %20 instead of as "+". We have to // fix that with String.replace() var pair = encodeURIComponent(name).replace(regexp,"+") + '=' + encodeURIComponent(value).replace(regexp,"+"); pairs.push(pair); } // Concatenate all the name/value pairs, separating them with & return pairs.join('&'); }; ***** end of older *working* Ajax setup ****