Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Putting HTML in inline JavaScript

A tag inside Inline Javascript string literal is interpreted by the HTML parser as a closing tag, causing syntax errors.
According to

Although the STYLE and SCRIPT elements use Cdata for their data model, for these elements, CDATA must be handled differently by user agents. Markup and entities must be treated as raw text and passed to the application as is. The first occurrence of the character sequence “” (end-tag open delimiter) is treated as terminating the end of the element’s content. In valid documents, this would be the end tag for the element.

In real world scenario web browsers only end parsing a CDATA script block on an actual close-tag.
Unfortunately, there is no such special handling for script blocks in XHTML, causing There are different approaches to avoid such problems:

1) Encode the HTML string in base64 and decode it when reading
2) Split the

var str = '' + 'script' + '>';

3) Use CDATA


This post first appeared on Vladimir Ivanov's Tech, please read the originial post: here

Share the post

Putting HTML in inline JavaScript


Subscribe to Vladimir Ivanov's Tech

Get updates delivered right to your inbox!

Thank you for your subscription