Home > Article > Web Front-end > innerHTML dynamically adds html code and scripts compatible with multiple browsers_javascript skills
Symptom: When setting a value for innerHTML of an element, if the provided HTML code contains js scripts, many times these scripts are invalid, or they are valid on some browsers but not on other browsers.
Cause: Different browsers have different ways of handling scripts inserted into innerHTML. After practice, it can be summarized as follows:
For IE, first of all, the script tag must have the defer attribute, and secondly, at the time of insertion, the node to which 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 the time of insertion.
Based on the above conclusion, the general setting innerHTML method is given: