InnerHTML dynamically adds HTML code and scripts that are compatible with multiple browsers

  • 2020-03-30 04:05:01
  • OfStack

Symptom: when setting the innerHTML value of an element, if the supplied HTML code contains js scripts, many times these scripts are not valid, or are valid in one browser but not in another.

Reason: different browsers have different ways of inserting the script in innerHTML. Through practice, it is summarized as follows:

For IE, first, the script tag must have the defer attribute, and second, at the time of insertion, the node to which the innerHTML belongs must be in the DOM tree.

For Firefox and Opera, the node to which innerHTML belongs cannot be in the DOM tree at insert time.

Related articles: