Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery remove script tags returned by AJAX requests?

Tags:

jquery

I'm performing an AJAX call in my jQuery-based JavaScript code. The response contains a <script> tag. However, it seems jQuery removes this tag.

Is this normal behavior of jQuery or XHR or a bug?

like image 450
hd. Avatar asked Apr 26 '11 07:04

hd.


1 Answers

It will depend on the dataType option you set in your AJAX call. If you set it to html it will give you back the HTML as plain text with the script tags ready for insertion to the DOM. If it is set to script then jQuery will attempt to run the script and return the result as a plain text string.

If you are not setting a dataType then jQuery will automatically infer the type from the response MIME type.

Have a look at the jQuery docs ( http://api.jquery.com/jQuery.ajax/ ) for more info on the dataType setting.

like image 126
Mark Keats Avatar answered Sep 24 '22 03:09

Mark Keats