Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JavaScript script tags inside jQuery .html() function? [duplicate]

Is it possible to get JavaScript script tags inside jQuery .html() function?

function pleaseWork(){
    $('#content').html('<h3 style="color:#335c91">This is the header</h3><div class="holder"><script type="text/javascript" src="javascriptFile.js"></script></div>');
}

What I am trying to accomplish is when the user clicks on a button, this header and JavaScript code show up in the div content, right now when I click on the button, there is no header showing and it goes to a blank page and shows the JavaScript code (it works, but not how I would like it to work.)

like image 717
user979331 Avatar asked May 21 '26 23:05

user979331


1 Answers

Escape the </script> tag, by replacing it with this: <\/script>
If that doesn't work, also surround the script with cdata tags.

Working demo: http://jsfiddle.net/BgxZN/1/

EDIT:
If you want to actually show the contents of the javascript file inside the div, you'll need to use an XMLHTTP request or an iframe.

New working demo: http://jsfiddle.net/BgxZN/13/

like image 61
someone Avatar answered May 24 '26 19:05

someone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!