{"id":309,"date":"2007-06-20T00:16:23","date_gmt":"2007-06-20T05:16:23","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2007\/06\/20\/chain-javascript-events\/"},"modified":"2009-07-21T22:22:00","modified_gmt":"2009-07-22T05:22:00","slug":"chain-javascript-events","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2007\/06\/20\/chain-javascript-events\/","title":{"rendered":"Chain JavaScript Events"},"content":{"rendered":"<p>In JavaScript there can be only one JavaScript onload event handler.  This is a somewhat an issue if you have all sort of third party libraries that want to run some piece of JavaScript code when the page finishes loading.  You can use the <a href=\"http:\/\/developer.yahoo.com\/yui\/event\">YUI! Event<\/a> library add and chain event listeners to a DOM element, but adding another resource dependency to you application might not always be the right answer.  I recently had to chain some onload event handlers for a simple script I was writing and I didn&#8217;t want to require any additional files.  Here is my poor man&#8217;s version of a JavaScript event chain gang.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ The loaders array will contain a\r\n\/\/ list of event handlers\r\nif(!window.loaders) {\r\n  window.loaders = new Array(0);\r\n}\r\n\/\/ Save the current event handler\r\nif(window.onload) {\r\n  window.loaders.push(window.onload);\r\n}\r\n\/\/ Attach new onload event handler\r\nwindow.onload = function() {\r\n  \/\/ Execute previous cached event handlers.\r\n  for(var i=0; i &lt; window.loaders.length; i++) {\r\n    var func = window.loaders&#x5B;i];\r\n    func();\r\n  }\r\n\r\n  \/\/ New event handler code goes here...\r\n}\r\n<\/pre>\n<p>With code like this you can guarantee that the old even handler code will run before your new code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript there can be only one JavaScript onload event handler. This is a somewhat an issue if you have all sort of third party libraries that want to run some piece of JavaScript code when the page finishes loading. You can use the YUI! Event library add and chain event listeners to a DOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[16,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-4Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/309"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":1,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":792,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/309\/revisions\/792"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}