{"id":2473,"date":"2013-05-14T18:08:37","date_gmt":"2013-05-14T18:08:37","guid":{"rendered":"http:\/\/www.webninjataylor.com\/library\/?p=2473"},"modified":"2014-06-24T21:20:45","modified_gmt":"2014-06-24T21:20:45","slug":"javascript-callback-functions","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/javascript-callback-functions\/","title":{"rendered":"JavaScript Callback Functions"},"content":{"rendered":"<ul>\n<li>\u00a0Functions are objects\n<ul>\n<li>A Function object contains a string which contains the Javascript code of the function<\/li>\n<li>For Javascript, the distinction between code and data is sometimes blurred<\/li>\n<li>One benefit of this function-as-object concept is that you can pass code to another function in the same way you would pass a regular variable or object (because the code is literally just an object)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>function some_function2(url, callback) {\r\n\tvar httpRequest; \/\/ create our XMLHttpRequest object\r\n\tif (window.XMLHttpRequest) {\r\n\t\thttpRequest = new XMLHttpRequest();\r\n\t} else if (window.ActiveXObject) {\r\n\t\t\/\/ Internet Explorer is stupid\r\n\t\thttpRequest = new\r\n\t\t\tActiveXObject(\"Microsoft.XMLHTTP\");\r\n \t}\r\n\r\n\thttpRequest.onreadystatechange = function() {\r\n\t\t\/\/ inline function to check the status\r\n\t\t\/\/ of our request\r\n\t\t\/\/ this is called on every state change\r\n\t\tif (httpRequest.readyState === 4 &amp;&amp;\r\n\t\t\t\thttpRequest.status === 200) {\r\n\t\t\tcallback.call(httpRequest.responseXML);\r\n\t\t\t\/\/ call the callback function\r\n\t\t}\r\n\t};\r\n\thttpRequest.open('GET', url);\r\n\thttpRequest.send();\r\n}\r\n\/\/ call the function\r\nsome_function2(\"text.xml\", function() {\r\n\tconsole.log(this);\r\n});\r\nconsole.log(\"this will run before the above callback\");<\/pre>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/recurial.com\/programming\/understanding-callback-functions-in-javascript\/\" target=\"_blank\">Understanding Callback Functions in JavaScript<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Functions are objects A Function object contains a string which contains the Javascript code of the function For Javascript, the distinction between code and data is sometimes blurred One benefit of this function-as-object concept is that you can pass code to another function in the same way you would pass a regular variable or object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[9],"class_list":["post-2473","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-javascript"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/2473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=2473"}],"version-history":[{"count":7,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/2473\/revisions"}],"predecessor-version":[{"id":3362,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/2473\/revisions\/3362"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=2473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=2473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=2473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}