Make WordPress Core

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: joshuapeck9898's profile joshuapeck9898 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: reporter-feedback
Focuses: javascript Cc:


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.

Change History (2)

#1 @sabernhardt
3 years ago

Hi and welcome to Trac!

I can think of two questions:

  1. Is the custom JS file included in the theme or in a plugin?
  2. Where is the existing DOM element that receives special content from the script (is it in the content, header, footer, widget area, etc.)?

It could be better if all elements exist in the DOM before running the script, but you may tried that already.

#2 @sabernhardt
3 years ago

  • Keywords reporter-feedback added
Note: See TracTickets for help on using tickets.