Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // constructor function with name of class. JScript does not really have classes and neither is this like a
- //PHP constructor class http://php.net/manual/en/language.oop5.decon.php
- function Ajax() {
- // properties which are variables or constants in PHP5
- this.req = null;
- this.url = null;
- this.method = 'GET';
- this.asynch = true;
- this.status = null;
- this.statusText = '';
- this.postData = null;
- this.readyState = null;
- this.responseText = null;
- this.responseXML = null;
- this.handleResp = null;
- this.responseFormat = 'text',
- // 'text', 'html', 'xml' or 'object'
- this.mimeType = null;
- }
- // End Constructor
- //Create XMLHttpRequest method with XMLHttpRequest object
- //Ajax.prototype.init
- //this.init = function() {
- Ajax.prototype.init = function () {
- if (!this.req) {
- try {
- //Try to create objects for Firefox, Safari, IE7, etc.
- this.req = new XMLHttpRequest();
- }
- catch(e) {
- try {
- //Try to create object for later versions of IE.
- this.req = new ActiveXObject('MSXML2.XMLHTTP');
- }
- catch(e) {
- try {
- //Try to create for early versions of IE.
- this.req = new ActiveXObject('Microsoft.XMLHTTP');
- }
- catch(e) {
- //Could not create XMLHttpRequest object.
- return false;
- }
- }
- }
- }
- return this.req;
- };
- //Sending a Request method
- Ajax.prototype.doReq = function() {
- if (!Ajax.prototype.init()) {
- alert('Could not create XMLHttpRequest object.');
- return;
- }
- //Setting up a request
- //open methods with method, url and asycn yes or no
- Ajax.prototype.req.open(this.method, this.url, this.asynch);
- //Make sure mimetype is OK
- if (this.mimeType) {
- try {
- req.overrideMimeType(this.mimeType);
- }
- catch(e) {
- //couldn't override MIME type ... IE6 or Opera?
- }
- }
- //var self = this;
- // fix loss-of-scope in inner function
- Ajax.prototype.req.onreadystatechange = function() {
- var resp = null;
- if (Ajax.prototype.req.readyState == 4) {
- //do stuff to handle response
- switch (this.reponseFormat) {
- case 'text':
- resp = this.req.responseText;
- break;
- case 'xml':
- resp = this.req.responseXML;
- break;
- case 'object':
- resp = req;
- break;
- }
- if (Ajax.prototype.status >= 200 && Ajax.prototype.req.status <= 299) {
- this.handleResp(resp);
- }
- else {
- Ajax.prototype.handleErr(resp);
- }
- }
- };
- Ajax.prototype.req.send(this.postData);
- };
- Ajax.prototype.handleErr = function() {
- var errorWin;
- try {
- errorWin = window.open('', 'errorWin');
- errorWin.document.body.innerHTML = this.responseText;
- }
- catch(e) {
- alert('An error occured, but this error message cannot be '
- + 'displayed. This is probably because of your browser\'s '
- + 'pop-up blocker. \n'
- + 'Please allow pop-ups from this website if you want to '
- + 'see the full error messages. \n'
- + '\n'
- + 'Status Code: ' + this.req.status + '\n'
- + 'Status description: ' + this.req.statusText);
- }
- };
- Ajax.prototype.abort = function() {
- if (this.req) {
- this.req.onreadystatechange = function() {};
- this.req.abort();
- this.req = null;
- }
- };
- Ajax.prototype.doGet = function (url, hand, format) {
- this.url = url;
- this.handleResp = hand;
- this.responseFormat = format || 'text';
- this.doReq();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement