[html5] Is it safe to remove an element from within the element?

Markus Ernst derernst at gmx.ch
Thu Dec 22 01:59:25 PST 2011


Hello

I hope my question is not too basic for this mailing list, resp. the 
answer too obvious - googling for it is difficult, as possible search 
terms lead to tons of tutorials on removing elements, but not discussing 
this question.

I have a message for non-JS UAs:

<div id="jsMsg">
   <p>Javascript is turned off or not available in your browser. While
     the basic functionalities will work like this, some functions, such
     as "cancel"-buttons, might not work as expected.</p>
   <script type="text/javascript">
     var jsMsg = document.getElementById("jsMsg");
     jsMsg.parentNode.removeChild(jsMsg);
   </script>
</div>

This looks like a clean solution to me, as it is removing from the DOM 
both the element and the script to remove it. I found no problems with 
this code in Firefox 5.0 and Internet Explorer 9.

Anyway, as the removing mechanism is triggered in the code before the 
element is actually closed, I ask myself whether this works by design, 
or just accidentally.

Thanks for a comment!
Markus



More information about the Help mailing list