If javascript bool is true, show HTML -
how show html based upon javascript boolean? have html shows "all ok" in span. need show html based upon result of javascript query.
what i'd (similar using server code)
<script> if (mypreviouslydefinedvariable == true){ <span>yes</span> } else { <span>no</span> } </script>
please note, whilst i'm showing simple span actual code large block of html. know "convert" html javascript hoping avoid this, similar rendering partial in mvc.net
you can't embed html directly javascript (it creates javascript syntax error illegal javascript). html page doesn't work way. can use document.write()
insert javascript page @ current point of execution or can use other dom manipulation methods modify current page script.
for example, this:
<script> if (mypreviouslydefinedvariable) { document.write("<span>yes</span>"); } else { document.write("<span>no</span>"); } </script>
or, since there reasons avoid document.write()
, this:
<span id="yesno"></span> <script> var text = mypreviouslydefinedvariable ? "yes" : "no"; document.getelementbyid("yesno").innerhtml = text; </script>
here's references on why may want avoid document.write()
(it can slow down rendering of page): don't docwrite scripts , optimizing pages speculative parsing.
if have large blocks of dissimilar html want conditionally include in page, can either dynamically load , insert content separate template file using ajax call retrieve or can include possible html page in mother page , hide/show relevant blocks. each of these techniques has it's advantages , disadvantages depend upon circumstances.
Comments
Post a Comment