{"id":24,"date":"2008-04-25T14:22:04","date_gmt":"2008-04-25T13:22:04","guid":{"rendered":"http:\/\/www.icantinternet.org\/?p=24"},"modified":"2016-07-20T06:50:34","modified_gmt":"2016-07-20T06:50:34","slug":"jumpstart-ajax-tutorial","status":"publish","type":"post","link":"https:\/\/beheydt.be\/en\/jumpstart-ajax-tutorial\/","title":{"rendered":"Jumpstart AJAX Tutorial"},"content":{"rendered":"<p>Days I&#8217;ve been searching after a decent, small, simple tutorial, to help me take the first steps into the World Of Ajax (Asynchronous JavaScript + XML).<br \/>\nNone did I find&#8230; Therefor, I decided to write this small jumpstart Ajax Tutorial Thingy.<br \/>\nFeel free to try it, comment on it, and spread it&#8230;<\/p>\n<p><!--adsense-->First&#8230; what the heck is AJAX? Well, it is a Football team in The Netherlands, Amsterdam. if you were looking for them, and landed here, Google&#8217;s got you fooled \ud83d\ude09<br \/>\nIs it coffee too, like JAVA is? No, not really, but let me explain: AJAX is a set of technologies (Asynchronous JavaScript + XML) that allow you to make a call to an http server<br \/>\n(most often a RSS feed or a webpage), grab the content and show that in your existing page.<br \/>\nGreat you say&#8230; PHP, ASP, JSP, Perl,&#8230; all can do THAT!<br \/>\nYou are sooooo right!!!<br \/>\nBut none can do that without reloading the page. Indeed, no refresh of your page needed. Consequence: services like email and such do not need to reload the whole shebang whenever<br \/>\nyou click something, which makes the whole experience faster, and better for well&#8230; your bandwidth counter \ud83d\ude42<\/p>\n<p>Let&#8217;s see what this AJAX stuff looks like in real life:<\/p>\n<p>&nbsp;<\/p>\n<p><em>function loadurl(dest) {<\/em><\/p>\n<p><em>try {<\/em><br \/>\n<em> \/\/XMLHttpRequest is for Mozilla and the likes. IE uses ActiveX. Using an if then on the object works best, better then checking which browser it is.<\/em><br \/>\n<em> xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<\/em><br \/>\n<em> } catch (e) {<\/em><br \/>\n<em> \/\/ This is where you get in to if your browser does not support AJAX. write a friendly errormessage here to tell the user to upgrade to the latest Firefox<\/em><br \/>\n<em> }<\/em><\/p>\n<p><em>\/\/ the object xmlhttp triggers an event on every status change. These events are then handled by the triggered() function.<\/em><br \/>\n<em> xmlhttp.onreadystatechange = triggered;<\/em><\/p>\n<p><em>\/\/ open is the function that will actually open your file on the server.<\/em><br \/>\n<em> xmlhttp.open(&#8220;GET&#8221;, dest);<\/em><\/p>\n<p><em>\/\/ send() well, sends the request. if this is a POST request you need<\/em><br \/>\n<em> \/\/ the post variables: send(&#8220;fname=john&amp;lname=doe)<\/em><br \/>\n<em> \/\/ Mozilla can deal with send(); however<\/em><br \/>\n<em> \/\/ IE needs to see a value here, so better to use send(null); in case of get (and thus no variables to send).<\/em><br \/>\n<em> xmlhttp.send(null);<\/em><br \/>\n<em> }<\/em><\/p>\n<p><em>function triggered() {<\/em><br \/>\n<em> \/\/ if the readyState code is 4 (Completed)<\/em><br \/>\n<em> \/\/ and http status is 200 (OK), our request was succesful, and we can get the response from: responseText indeed \ud83d\ude09<\/em><br \/>\n<em> \/\/ other readyState codes that can be usefull are::<\/em><br \/>\n<em> \/\/ 0=Uninitialised 1=Loading 2=Loaded 3=Interactive<\/em><br \/>\n<em> if ((xmlhttp.readyState == 4) &amp;&amp; (xmlhttp.status == 200)) {<\/em><br \/>\n<em> \/\/ xmlhttp.responseText object holds the response for you to cherish, cuddle, and use in your code.<\/em><br \/>\n<em> document.getElementById(&#8220;output&#8221;).innerHTML = xmlhttp.responseText;<\/em><br \/>\n<em> }<\/em><br \/>\n<em> }<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>This piece of code needs to be placed between the html tags.<br \/>\nin the html body you need to call the function, and place the result in a way like this:<br \/>\n<em>&lt;p id=&#8221;AjaxOutput&#8221; onclick=&#8221;loadurl(&#8216;\/whatever.txt&#8217;)&gt;click here to load the whateverfile right here!&lt;\/p&gt;<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>This whateverfile can be any text file, but also any script that returns an output, making this technologie very usefull!<br \/>\ndo note that the destination url has to be in the same domain as the html file, or a security error can occur, depending on your security settings.<br \/>\nTo see all that is possible with AJAX, check out Google&#8217;s automated searchbox-completion, gmail, yahoo mail, &#8230;<br \/>\nThe list is endless, the sky is not even the limit&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>2016 Update: I know, all of this can easily be done using <a href=\"http:\/\/jquery.com\">jQuery<\/a>, and you are right! But sometimes it is not feasible to use jQuery, and, above all, it is never a bad thing to know the mechanics behind libraries such as jQuery.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Days I&#8217;ve been searching after a decent, small, simple tutorial, to help me take the first steps into the World Of Ajax (Asynchronous JavaScript + XML). None did I find&#8230; Therefor, I decided to write this small jumpstart Ajax Tutorial Thingy. Feel free to try it, comment on it, and spread it&#8230; First&#8230; what the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[921,942],"tags":[875],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-icantinternet-en","category-webdevelopment-icantinternet-en","tag-webdevelopment"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbkKxJ-o","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":0,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}