js-ajax-class

Gesponsorde koppelingen

PHP script bestanden

  1. js-ajax-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Function.prototype.bind = function( object )
{
    var __method = this, args = [];
    return function() {
        return __method.apply(object, args.concat(arguments));
    }
}

var Ajax = function( f_szTarget, f_arrOptions )
{
    // C O N S T R U C T O R //
    this.setTarget(f_szTarget);

    if ( !this.getTransport() )
    {
        alert('No AJAX supported for this browser!');
    }

    this.setOptions(f_arrOptions);

    this.request();
};

Ajax.prototype = {
    // MEMBERS //

    // PROPERTIES
    /**
     * @brief        xmlhttp
     * @type        object
     * @desc        The XmlHttp object that actually does the request. What kind of object this is, depends on the browser used.
     *
     */
    'xmlhttp'        : null,
    /**/

    /**
     * @brief        target
     * @type        string
     * @desc        The URL of the page the request is sent to. The HTTP headers are built by XmlHttp (browser dependant).
     *
     */
    'target'        : '',
    /**/

    /**
     * @brief        asynchronous
     * @type        boolean
     * @desc        Whether the ajax request is asynchronous. Since the A in _A_jax stands for _A_synchronous, the default is set to true.
     *
     */
    'asynchronous'    : true,
    /**/

    /**
     * @brief        method
     * @type        string
     * @desc        The HTTP request method.
     *
     */
    'method'        : 'POST',
    /**/

    /**
     * @brief        params
     * @type        string
     * @desc        The query string to act as post body.
     *
     */
    'params'        : '',
    /**/

    /**
     * @brief        onComplete
     * @type        function
     * @desc        The function to execute when the ajax request is made and status 4 is returned.
     *
     */
    'onComplete'    : function(){},
    /**/

    /**
     * @brief        busy
     * @type        float
     * @desc        The 'session id' of the current 'session'.
     *                 It's compared to Ajax.busy (the static one).
     *
     */
    'busy'            : 0,
    /**/


    // METHODS //
    'getTransport': function()
    {
        try {
            this.xmlhttp = new XMLHttpRequest();
        } catch (e1) {
            try {
                this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e2) {
                try {
                    this.xmlhttp = new XMLHttpRequest("Microsoft.XMLHTTP");
                } catch (e3) {
                    this.xmlhttp = false;
                }
            }
        }

        return !!this.xmlhttp;
    },

    'setTarget': function( f_szTarget )
    {
        if ( !f_szTarget )
        {
            f_szTarget = document.location;
        }
        else if ( '?' == f_szTarget.substr(0,1) )
        {
            f_szTarget = document.location + "" + f_szTarget;
        }

        this.target = "" + f_szTarget;
        return true;
    },

    'setOptions': function( f_arrOptions )
    {
        // Method
        if ( f_arrOptions['method'] )
        {
            this.method = f_arrOptions['method'];
        }

        // Parameters
        if ( f_arrOptions['params'] )
        {
            this.params = f_arrOptions['params'];
        }
/*        else if ( f_arrOptions['parameters'] )
        {
            this.params = f_arrOptions['parameters'];
        }*/

        // Completion function
        if ( f_arrOptions['onComplete'] && "function" == typeof f_arrOptions['onComplete'] )
        {
            this.onComplete = function(){ f_arrOptions['onComplete']( this.xmlhttp ) };
        }
/*        else if ( f_arrOptions['onCompletion'] && "function" == typeof f_arrOptions['onCompletion'] )
        {
            this.onComplete = function(){ f_arrOptions['onCompletion']( this.xmlhttp ) };
        }*/

        // Asynchronous
        if ( f_arrOptions['async'] )
        {
            this.asynchronous = f_arrOptions['async'];
        }

        return true;
    },

    'request': function()
    {
        // Create 'session id'
        rnd            = Math.random();
        Ajax.busy    = rnd;
        this.busy    = rnd;

        // Start request
        this.xmlhttp.open(
            this.method.toUpperCase(),
            this.target,
            this.asynchronous
        );
        if ( 'POST' == this.method.toUpperCase() )
        {
            this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        this.xmlhttp.onreadystatechange = this.whileRequestHandler.bind(this);
        this.xmlhttp.send(this.params);
    },

    'whileRequestHandler': function()
    {
        // Ajax statusses:
        // 0 - Means it's ready to go (uninitialized)
        // 1 - Loading
        // 2 - Finished loading
        // 3 - Almost ready for use
        // 4 - Loading is complete and ready to be dealt with.

        if ( 1 == this.xmlhttp.readyState )
        {
            Ajax.arrHandlers['onStart'](this.xmlhttp); // You might want to _not_ pass this.xmlhttp to the onStart handler function
        }
        else if ( 4 == this.xmlhttp.readyState )
        {
            // Execute the user's onComplete post-ajax function
            this.onComplete();

            if ( this.busy == Ajax.busy ) Ajax.busy = 0;

            // Execute the user's onComplete handler
            Ajax.arrHandlers['onComplete'](this.xmlhttp); // You might want to _not_ pass this.xmlhttp to the onComplete handler function
        }
    }

};

// STATIC METHODS //
Ajax.busy            = 0;
Ajax.arrHandlers    = { 'onStart' : function(){}, 'onComplete' : function(){} };
Ajax.setHandlers    = function( f_arrHandlers )
{
    if ( "object" != typeof f_arrHandlers ) return false;

    if ( f_arrHandlers['onStart'] && "function" == typeof f_arrHandlers['onStart'] )
    {
        Ajax.arrHandlers.onStart = f_arrHandlers['onStart'];
    }

    if ( f_arrHandlers['onComplete'] && "function" == typeof f_arrHandlers['onComplete'] )
    {
        Ajax.arrHandlers.onComplete = f_arrHandlers['onComplete'];
    }

    return true;
};

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.