{"id":81,"date":"2008-09-23T07:04:22","date_gmt":"2008-09-23T12:04:22","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/23\/javascript-events-the-dom-and-firefoxgecko\/"},"modified":"2008-09-23T07:04:22","modified_gmt":"2008-09-23T12:04:22","slug":"javascript-events-the-dom-and-firefoxgecko","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/23\/javascript-events-the-dom-and-firefoxgecko\/","title":{"rendered":"Javascript Events, the DOM, and Firefox\/Gecko"},"content":{"rendered":"<p>OK, here&#8217;s the deal, as quick as I can put it, but with enough words that Google will correctly index this post and make this information easier to find for the next person who needs it.<\/p>\n<p>Let&#8217;s say you&#8217;re developing a Firefox extension, and you need to look at the HTML of the page. You know that Firefox is constantly throwing event notifications &#8212; there&#8217;s the MouseEvent &#8220;click&#8221; when something is clicked, the KeyboardEvent &#8220;keyup&#8221; when a key is release, and so on.<\/p>\n<p>So, you figure &#8220;well, I need the HTML of the page, so I&#8217;ll wait until it loads, then grab the source\/parse the DOM tree\/unleash flying monkeys&#8221;. You know that the style of<\/p>\n<blockquote><p>window.onload = the_function_name;<\/p><\/blockquote>\n<p>is bad, because it only lets one function receive the load event. So, you write some code like<\/p>\n<blockquote><p>gBrowser.addEventListener(&#8220;load&#8221;, the_function_name, true);<\/p><\/blockquote>\n<p>because this is what you want. You wait for the &#8220;load&#8221; event to fire, and then run your function. Test this code out, and it will work well-enough, most of the time. Sometimes, it lags, sometimes it doesn&#8217;t even fire at all. WTF? Well, <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/15\/detecting-page-loads-in-a-firefox-extension\/\">I&#8217;ve been there<\/a>.<\/p>\n<p>Here&#8217;s the thing: the &#8220;load&#8221; event fires when loading is done. If you&#8217;re looking at the whole window\/document, it&#8217;s not done loading until the whole document is loaded, including bandwith-and-time-heavy images. And these things can be slow to load, or hang entirely. This can seriously mess with the concept of &#8220;loading&#8221;. You don&#8217;t care about any of these things, you just want the HTML.<\/p>\n<p>Well, Firefox is a very deep project, and there&#8217;s a few <a href=\"http:\/\/developer.mozilla.org\/en\/Gecko-Specific_DOM_Events\">secret events<\/a> that aren&#8217;t widely used or documented. If you want to know when the HTML is loaded &#8212; or more accurately, when the DOM Tree&#8217;s content is loaded &#8212; there&#8217;s a special event you can monitor: &#8220;DOMContentLoaded&#8221;<\/p>\n<blockquote><p>gBrowser.addEventListener(&#8220;DOMContentLoaded&#8221;, the_function_name, true);<\/p><\/blockquote>\n<p>This is a very reliable way of knowing when the browser has loaded only the page. The catch is that it&#8217;s Firefox-internal only. Which means it only works from an extension &#8212; you can&#8217;t use this in a web page. There are techniques for duplicating this functionality in other browsers, but that&#8217;s getting beyond this post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OK, here&#8217;s the deal, as quick as I can put it, but with enough words that Google will correctly index this post and make this information easier to find for the next person who needs it. Let&#8217;s say you&#8217;re developing a Firefox extension, and you need to look at the HTML of the page. You &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/23\/javascript-events-the-dom-and-firefoxgecko\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Javascript Events, the DOM, and Firefox\/Gecko&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145,236,123],"tags":[241,238,254,251,250,60,253,252,237],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}