JQuery.replaceWith does not accept <script> tags

I have an AJAX response containing some HTML, including <script> elements.

This HTML should replace the existing element, but when I use:

 $(element).replaceWith($(response)) 

I am losing script tags from it ...

How can I insert HTML exactly how it is sent?

+4
source share
3 answers

This code is from the facebook application sample. Paste <div id="fb-root"></div> into the document. For this, the code requires jQuery to load the script. In your case, replace //connect...../all.js ' with <script> elements.

 (function() { var e = document.createElement('script'); e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); 
+1
source

It will return a string !!

 $.get("ajax.php") .success(function(returningHtmlString) { $(element).replaceWith(returningHtmlString); }); 
+2
source

See jQuery.parseHTML( data [, context ] [, keepScripts ] )

Following should work

 var content = jQuery.parseHTML(response, document, true) $(element).replaceWith(content) 
0
source

Source: https://habr.com/ru/post/1381038/


All Articles