Rajat Gupta
Rajat Gupta

Reputation: 26607

HTML elements inside <script/> getting automatically commented

I have an xhtml page with following:

<script type="text/html" id="test1">
    <li>
        test
    </li>
</script>

However when the page is loaded in browser(testing in Google Chrome) I see it as following:

<script type="text/html" id="test1"><!--    

        <li>test

//--></li>

    <!--   

        <li>test

//--></script>

Why this is happening & how to prevent this ? This doesn't happen if page is html instead of xhtml.

--

Upvotes: 1

Views: 157

Answers (1)

Jason
Jason

Reputation: 3360

XHTML is #PCDATA while HTML is CDATA. In order to keep that code from being parsed, you have to wrap it in CDATA tags.

<script type="text/javascript">//<![CDATA[
<li>
    test
</li>

//]]></script> 

Even if that works though, I'm not sure why you'd want to have HTML inside script tag, it's invalid code and doesn't make a lot of sense.

Upvotes: 1

Related Questions