{"id":174,"date":"2008-04-24T00:30:00","date_gmt":"2008-04-24T00:30:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=174"},"modified":"2008-04-24T00:30:00","modified_gmt":"2008-04-24T00:30:00","slug":"nested-javascript-inclusions-and-ie","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=174","title":{"rendered":"Nested JavaScript inclusions and IE"},"content":{"rendered":"<p>Consider the following scenario: file.html contains inline JS code to load remote.js and call a function in remote.js<\/p>\n<p><textarea name=\"code\" class=\"js\"><br \/>\n<script text=\"text\/javascript\"><\/p>\n<p>alert('Before loading remote.js');\ndocument.write('<sc'+'ript type=\"text\/javascript\" src=\"remote.js\"><\\\/script>');<\/p>\n<p>alert('After loading remote.js');\ndocument.write('<sc'+'ript type=\"text\/javascript\">remoteFunction();<\\\/script>');<\/p>\n<p><\/script><br \/>\n<\/textarea><\/p>\n<p>And in remote.js<br \/>\n<textarea name=\"code\" class=\"js\"><\/p>\n<p>function remoteFunction() {<br \/>\n   alert(&#8216;In remote function&#8217;);<br \/>\n}<\/p>\n<p><\/textarea><\/p>\n<p>You would expect the output in this order and Firefox actually honors this order correctly:<\/p>\n<ol>\n<li>Before loading remote.js<\/li>\n<li>After loading remote.js<\/li>\n<li>In remote function<\/li>\n<\/ol>\n<p>IE on the other hand will give you an error (&#8220;Object Expected&#8221;) and will call the function prematurely. Meaning that it executes before loading remote.js. This is an unexpected behavior. Fortunately, you can add the attribute defer=&#8221;defer&#8221;. This will specifically prevent IE from executing and actually produce proper result. Hmm, it&#8217;s IE! It has its own style!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following scenario: file.html contains inline JS code to load remote.js and call a function in remote.js And in remote.js function remoteFunction() { alert(&#8216;In remote function&#8217;); } You would expect the output in this order and Firefox actually honors this order correctly: Before loading remote.js After loading remote.js In remote function IE on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/174"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=174"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}