« FireBug, AJAX debugging tool for Firefox and much more | Main | AJAX Webmail, the remote scripting experience with email »

ezRemoteScripter, a little AJAX remote scripting helper

ezRemoteScripter is a javascript wrapper around the XmlHttpRequest object, even that the idea of AJAX is easy to implement we are seeing more and more implementation from authors that don't find the features they're looking for in current impelementation, as Khair-ed Din Husseini the author of ezRemoteScripter said :

A couple of days ago I was browsing the net for some remote scripting (also known as AJAX) examples and how to use the XmlHttpRequest object. However, all examples were quite nasty in implementation so I decided to write an nice little javascript wrapper around the XmlHttpRequest object.

ezRemoteScripter main javascript is the following :

// JavaScript Document
function EzRemoteScripter() {
    //private variables
    var _XmlHttpRequest = null;
    var _This = null;
   
    //public properties
    this.GetResponseXML = function() {
        return (_XmlHttpRequest) ? _XmlHttpRequest.responseXML : null;
    }
   
    this.GetResponseText = function() {
        return (_XmlHttpRequest) ? _XmlHttpRequest.responseText : null;
    }
   
    this.GetXMLHttpRequestObj = function() {
        return _XmlHttpRequest;
    }
   
    //public methods
    this.InitXmlHttpRequest = function(Method, CallUri) {
        _InitXmlHttpRequest();
        _This = this;
        switch( arguments.length ) {
            case 2:
                _XmlHttpRequest.open(Method, CallUri);   
                break;
            case 3:
                _XmlHttpRequest.open(Method, CallUri, arguments[2]);
                break;
        }
       
        if( arguments.length >= 4) {
            _XmlHttpRequest.open(Method, CallUri, arguments[2], arguments[3]);
        }
    }
   
    this.Commit = function(Data) {
        if( _XmlHttpRequest ) {
            _XmlHttpRequest.send(Data);
        }
    }
   
    this.Close    = function() {
        if( _XmlHttpRequest ) {
            _XmlHttpRequest.abort();
        }
    }
   
    //public events
    this.OnUninit         = function() {};
    this.OnLoading         = function() {};
    this.OnLoaded         = function() {};
    this.OnInteractive     = function() {};
    this.OnSuccess        = function() {};
    this.OnFailure         = function() {};
   
    //private events
    function _OnUninit()         { _This.OnUninit(); };
    function _OnLoading()         { _This.OnLoading(); };
    function _OnLoaded()         { _This.OnLoaded(); };
    function _OnInteractive()     { _This.OnInteractive(); };
    function _OnSuccess()         { _This.OnSuccess(); };
    function _OnFailure()         { _This.OnFailure(); };
   
    //private methods
    function _InitXmlHttpRequest() {
        _XmlHttpRequest = _GetXmlHttpRequest();
        _XmlHttpRequest.onreadystatechange = _StateHandler;
    }
   
    function _StateHandler() {
        switch(_XmlHttpRequest.readyState) {
            case 0:
                window.setTimeout("void(0)", 100);
                _OnUninit();
                break;
            case 1:
                window.setTimeout("void(0)", 100);
                _OnLoading();
                break;
            case 2:
                window.setTimeout("void(0)", 100);
                _OnLoaded();
                break;
            case 3:
                window.setTimeout("void(0)", 100);
                _OnInteractive();
                break;
            case 4:
                if (_XmlHttpRequest.status == 200) {
                    _OnSuccess();
                } else {
                    _OnFailure();
                }
                return;
                break;
        }
    }
   
    function _GetXmlHttpRequest() {
        var requester
        try {
            requester = new XMLHttpRequest();
        } catch (error) {
            try {
                requester = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (error) {
                return null;
            }
        }
        return requester;
    }
} 

The code is quiet selfexplanatory as he noted, Husseini didn't setup a website for the script or a download page so I just created this for you so you can give it a test with the demo code and ezRemoteScripter.

Download ezRemoteScripter

Bookmark this article at these sites
Post a comment





(Email will remain hidden)





Please enter the security code you see here




Related entries
Email to a friend
Email this article to:


Your email address:


Message (optional):