Suppose I have a web document with alternate content, that is, if some condition is met, it should display content A, and if the condition isn't met, it should display content B. That's easy to implement - I wrap each content with a <div> tag, setting its style to display:none, and have some JS code that changes one of them to displayed according to the condition.
But suppose content A and content B aren't just plain html. Each of them contains <script> tags. I want these scripts to be in effect only if the corresponding content is displayed, that is, if my condition is met, I want all the <script>s in content A to be in effect, and all the <script>s in content B to be ignored, and vice versa.
Actually there're two questions here. Firstly, i want the <script>s in (say) content A to be compiled (and run) only when I tell them so, that is, after I decide that my condition is true. Secondly, when the condition becomes false, I want all the functions defined in these scripts to be undefined, and instead of them, I need to compile and run all the scripts in content B (the condition may become true again later, and then false again, and so on).
How can I do it?