ajax-class

Gesponsorde koppelingen

PHP script bestanden

  1. 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>AJAX</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script type="text/javascript">

            function AjaxObject()
            {
                // Init the HTTPREQUEST
                function funcInitHttpRequest()
                {
                    if(window.XMLHttpRequest) // Mozilla, Safari, etc
                    {
                        try
                        {
                            oHttpRequest = new XMLHttpRequest();
                            if(oHttpRequest.overrideMimeType)
                            {
                                oHttpRequest.overrideMimeType('text/xml');
                            }
                        }
                        catch(e)
                        {
                            oHttpRequest = null;
                        }
                    }
                    else if(window.ActiveXObject) // Internet Explorer
                    {
                        try
                        {
                            oHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
                        }
                        catch(e)
                        {
                            try
                            {
                                oHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
                            }
                            catch(e)
                            {
                                oHttpRequest = null;
                            }
                        }
                    }
                }

                // See if AJAX is available
                function funcIsAjaxEnabled()
                {
                    if(oHttpRequest == null)
                    {
                        return false;
                    }

                    return true;
                }

                // See if an error occured
                function funcIsErrorResponse()
                {
                    if(funcIsAjaxEnabled())
                    {
                        if(oHttpRequest.readyState == 4)
                        {
                            if((oHttpRequest.status != 200))
                            {
                                return true;
                            }
                        }
                    }

                    return false;
                }

                // See if a valid response was recieved
                function funcIsValidResponse()
                {
                    if(funcIsAjaxEnabled())
                    {
                        if(oHttpRequest.readyState == 4)
                        {
                            if((oHttpRequest.status == 200))
                            {
                                return true;
                            }
                        }
                    }

                    return false;
                }

                // Return the response as a string
                function funcGetResponseAsText()
                {
                    if(funcIsValidResponse()) // A valid responce was recieved.
                    {
                        return oHttpRequest.responseText;
                    }

                    return '';
                }

                // Get the current state of the AJAX object
                function funcGetState()
                {
                    if(funcIsAjaxEnabled())
                    {
                        return oHttpRequest.readyState;
                    }

                    return 0;
                }

                // Get the current status of the AJAX object
                function funcGetStatus()
                {
                    if(funcIsAjaxEnabled())
                    {
                        return oHttpRequest.status;
                    }

                    return 0;
                }

                // Try to send a new request (url + return function)
                function funcSendRequest(sUrl, sFunction)
                {
                    if(funcIsAjaxEnabled())
                    {
                        try
                        {
                            oHttpRequest.onreadystatechange = sFunction;
                            oHttpRequest.open('GET', sUrl, true);
                            oHttpRequest.send(null);

                            return true;
                        }
                        catch(e)
                        {
                            alert('Cannot open URL: ' + sUrl);
                        }
                    }
                    else
                    {
                        alert('AJAX not available.');
                    }

                    return false;
                }

                // A reference to the HttpRequest object
                var oHttpRequest = null;

                // Define class methods
                this.enabled = funcIsAjaxEnabled;
                this.error = funcIsErrorResponse;
                this.request = funcSendRequest;
                this.response = funcIsValidResponse;
                this.state = funcGetState;
                this.status = funcGetStatus;
                this.text = funcGetResponseAsText;

                // Init oHttpRequest
                funcInitHttpRequest();
            }

            // My function to catch the ajax response
            function catchAjaxResponse()
            {
                if(oAjax.response()) // See if a valid response was returned
                {
                    document.getElementById('ajax_result_text').innerHTML = escapeHtml(oAjax.text());
                }
                else if(oAjax.error()) // An error then?
                {
                    alert('An error occured while retrieving data. Please ensure that the requested url is valid.');
                }
            }

            // Function to escape HTML chars: &, <, >, "
            function escapeHtml(string)
            {
                string = string.replace(/[&]/g, '&amp;');
                string = string.replace(/[<]/g, '&lt;');
                string = string.replace(/[>]/g, '&gt;');
                string = string.replace(/[\"]/g, '&quot;');

                return string;
            }

            // Function to load a new URL
            function loadUrl()
            {
                var url = document.getElementById('url').value;
                oAjax.request(url, catchAjaxResponse);
            }

            var oAjax = new AjaxObject();

        </script>
    </head>
    <body>

        <p>Laad een URL met AJAX</p>
        <table border="0" cellpadding="5" cellspacing="0"><tr><td align="left" valign="top">Url</td><td align="left" valign="top"><input id="url" name="url" size="60" value="http://"></td><td align="left" valign="top"><input onclick="javascript: loadUrl();" type="button" value="load"></td></tr></table>
        <hr>
        <pre id="ajax_result_text">
            &nbsp;
        </pre>

    </body>
</html>

 
 

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.