Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery html() strips out script tags

I need to replace the content of a div in my page with the html resultant from an ajax call. The problem is that the html have some necessary scripts in it and it seems that jquery html() function stripts them out, I need to filter the response and only get a specific div.

I am thinking on a workaround which is to extract all the script tags from the ajax response and then append them do the DOM but i am having trouble doing that.

Here is my code;

   $('a.link-vote').live('click',function(){         var idfeedback = $(this).attr('id').split('-')[1];         var href = $(this).attr('href');         $('.feedback-' + idfeedback + '-loader').show();         $.ajax({             type: "POST",             url: href,             success: function(response){                var x = $(response).find('#feedback-'+ idfeedback).html();                $('.feedback-' + idfeedback + '-loader').hide();                $('#feedback-'+ idfeedback).html(x);              }         });         return false;     }); 

I found this old topic: jQuery - script tags in the HTML are parsed out by jQuery and not executed

but the is any conclusion. I tried the solutions suggested there but none of them work.

EDIT: I seem to found a workaround based on that old topic but it´s not pretty;

  var dom = $(response);                 // var x = $(response).find('#feedback-'+ idfeedback).html();                 $('.feedback-' + idfeedback + '-loader').hide();                 //$('#feedback-'+ idfeedback).html(x);                  $('#feedback-'+ idfeedback).html(dom.find('#feedback-'+ idfeedback).html());                  dom.filter('script').each(function(){                     var obj = $(this);                     $('#feedback-'+ idfeedback + ' .feedback-comments').append(obj);                 }); 

There must be a easy way.

like image 667
brpaz Avatar asked Nov 02 '10 15:11

brpaz


People also ask

Can I put script tag outside body?

JavaScript files have file extension . js . To use an external script put the name of the script file in the src attribute of a script tag. External scripts cannot contain script tags.

Can we write jQuery in script tag?

Step 1: Open the HTML file in which you want to add your jQuery with the help of CDN. Step 2: Add <script> tag between the head tag and the title tag which will specify the src attribute for adding your jQuery. Step 3: After that, you have to add the following path in the src attribute of the script tag.

Does jQuery go inside script tag?

We can include the jQuery CDN URL in the script tag and use jQuery in HTML. We can either write the jQuery in a separate . js file and include it in the HTML file or write the jQuery inside the script tag. First, go to the JQuery CDN website and choose the latest stable version of the jQuery.

Can you put script tags in body?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.


1 Answers

Edit: I'm tired and not thinking. You can just use the native innerHTML method instead of .html():

$('#feedback-' + idfeedback)[0].innerHTML = x; 

Original answer:

My hunch is that the answer you linked doesn't work for you because the included scripts are called with a src attribute rather than script content between the <script> and </script> tags. This might work:

$.ajax({     url: 'example.html',     type: 'GET',     success: function(data) {          var dom = $(data);          dom.filter('script').each(function(){             if(this.src) {                 var script = document.createElement('script'), i, attrName, attrValue, attrs = this.attributes;                 for(i = 0; i < attrs.length; i++) {                     attrName = attrs[i].name;                     attrValue = attrs[i].value;                     script[attrName] = attrValue;                 }                 document.body.appendChild(script);             } else {                 $.globalEval(this.text || this.textContent || this.innerHTML || '');             }         });          $('#mydiv').html(dom.find('#something').html());      } }); 

Note, this has not been tested for anything and may eat babies.

like image 102
eyelidlessness Avatar answered Sep 23 '22 00:09

eyelidlessness