Opened 3 years ago
Last modified 3 years ago
#54179 new defect (bug)
Raw JS is prematurely ended with a </script> tag when HTML tags are present within JS strings.
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | reporter-feedback |
Focuses: | javascript | Cc: |
Description
We are using a custom JS file that is reaching out to a web service, building a string from the data, and setting it as the innerHTML to a DOM Element already on the page. For some reason, HTML tags in this string were causing our script to get cut short, and whatever remained would instead by interpreted as HTML. A JavaScript snippet such as the following:
myElement.innerHTML = ‘<div><p>My Content</p></div>’;
would cause a </script> tag to be inserted between the </p> and </div> tags, cutting the JS short, leaving the rest to be read as HTML. The fix was to replace every tag in these strings with a chain of concatenations:
myElement.innerHTML = ‘<’ + ‘div>’ + ‘<’ + ‘p>My Content’ + ‘<’ + ‘/p>’ + ‘<’ + ‘/div>’;
I don’t know what was causing this, but this workaround prevented whatever was processing our JavaScript from recognizing and interpreting the HTML tags.
Hi and welcome to Trac!
I can think of two questions:
It could be better if all elements exist in the DOM before running the script, but you may tried that already.