{"id":221,"date":"2012-05-24T07:37:03","date_gmt":"2012-05-24T14:37:03","guid":{"rendered":"http:\/\/shanelabs.com\/blog\/?p=221"},"modified":"2012-05-24T07:37:03","modified_gmt":"2012-05-24T14:37:03","slug":"does-hidden-javascript-still-run","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2012\/05\/24\/does-hidden-javascript-still-run\/","title":{"rendered":"Does hidden javascript still run?"},"content":{"rendered":"<p>I was curious if javascript was still executed by the browser even if it was within an element that is hidden in the DOM. A quick search yielded no conclusive evidence, so I decided to test myself.<\/p>\n<p>Quick answer: Yes, it does still execute &#8211; for both inline and included javascript regardless of how it&#8217;s hidden (display:none or visibility:hidden).<\/p>\n<p>I made a simple test script to see if scripts still run if hidden. Then I tested it on:\u00a0Internet Explorer 7, 8, 9, Chrome, Safari, and Firefox and all work the same way.<\/p>\n<p>Here&#8217;s the test script I used. Within each browser, I got 7 popup windows. In case you&#8217;re wondering. I wanted to test to see if including a javascript made any difference, so the &#8216;testjs.js&#8217; script just makes a simple alert call.<\/p>\n<blockquote>\n<pre>\r\n&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xml:lang=\"en\"&gt;\r\n&lt;head&gt;\r\n                &lt;script&gt;\r\n                alert('head');\r\n                &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n                &lt;div&gt;\r\n                                visible div\r\n                                &lt;script&gt;alert('visible div');&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div style=\"display:none\"&gt;\r\n                                display:none\r\n                                &lt;script&gt;alert('displaynone div');&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div style=\"visibility:hidden\"&gt;\r\n                                visibility:hidden\r\n                                &lt;script&gt;alert('invisible div');&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n               \r\n               \r\n                &lt;div&gt;\r\n                                visible div\r\n                                &lt;script src=\"testjs.js\"&gt;&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div style=\"display:none\"&gt;\r\n                                display:none\r\n                                &lt;script src=\"testjs.js\"&gt;&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div style=\"visibility:hidden\"&gt;\r\n                                visibility:hidden\r\n                                &lt;script src=\"testjs.js\"&gt;&lt;\/script&gt;\r\n                &lt;\/div&gt;\r\n \r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I was curious if javascript was still executed by the browser even if it was within an element that is hidden in the DOM. A quick search yielded no conclusive evidence, so I decided to test myself. Quick answer: Yes, it does still execute &#8211; for both inline and included javascript regardless of how it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-software","category-web"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":8,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/229"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}