jQuery html() method ignores script tags from HTML and not executed.

This is a limitation of jQuery .html() method. When we pass a string HTML block to inject into DOM using .html() method, it injects the HTML block but the script tags inside the HTML block do not execute.

We were inserting a piece of HTML block that had an analytics script using jQuery html() method. The HTML block was injected but the analytics script didn’t work. Here is how we solved it:

We had to use $.parseHTML of jQuery to fix the issue. Following the code segment.

 $(".container").html($.parseHTML(HTMLblockWithScriptTag, document, true));
// $.parseHTML method's third parameters does the trick

jQuery – script tags in the HTML are parsed out by jQuery and not executedhttps://api.jquery.com/jQuery.parseHTML/