1

It appears that the .html function doesn't return javascript. Is there a function that returns both javascript and html?

EDIT:

Example:

Main Page

$.ajax({        
  url: 'ajaxpage.php',
  type: 'post',
  data: postvars,
  cache: false,
  dataType: 'html',
  success: function (html) {
    $('#content').html($(html).find('#content').html());
  }
});

ajaxpage.php

<html>
<body>
<div id="content">
   //SOME HTML
   <script type="text/javascript">
      //SOME JAVASCRIPT
   </script>
</div>
</body>
</html>

$(html).find('#content').html() returns

   //SOME HTML

while I want

   //SOME HTML
   <script type="text/javascript">
      //SOME JAVASCRIPT
   </script>
fanfavorite
  • 5,128
  • 1
  • 31
  • 58

4 Answers4

1

Unfortunately it doesn't appear that there is something that will allow the javascript to stay using jquery. Most functions strip out the javascript and others show the whole page, which just using the html variable from the success function would do.

The returned html variable has the javascript and you can just strip out what you don't need from there.

var htmlcontent = html.match(/<div\s+id="content">[\S\s]*?<\/div>/);
if (htmlcontent != null) {
   $('#content').html(htmlcontent[0]);
}
fanfavorite
  • 5,128
  • 1
  • 31
  • 58
0

Try using .load() instead of .html()

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • `.load` initiates an ajax request. The questioner is trying to return the contents of a tag, not get more contents from the server. – Nick Brunt May 16 '11 at 22:39
0

Looks like some of jQuery's insertion methods (like html or append) process the code for possible injections, removing script tags along the way. You can take a look at the answers to this question, or see the relevant comment in the jQuery API docs.

Community
  • 1
  • 1
Dan
  • 537
  • 2
  • 7
  • hmmm interesting. I couldn't get anything to work like that. Seemed to be stripping out the javascript created script or not executing the create script. – fanfavorite May 17 '11 at 01:58
0

I have tried with filter but it only return the html part

 $('#content').html($(html).filter('#content'));

But when I tried with filter along with prevObject

i.e

$('#content').html($(html).filter('#content').prevObject);

it returns the html part (visible in firebug) as well as the javascript(not visible in firebug).

Nazmul
  • 7,078
  • 12
  • 51
  • 63
  • What is prevObject? This seems to process the javascript, but returns the whole page, rather than just the content div. Perhaps prevObject is a plugin or extension of some kind? – fanfavorite May 17 '11 at 01:57
  • prevObject is not a plugin. http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/, You are right, it returns the whole page.(I was testing only with the script and the html part within the content div) – Nazmul May 17 '11 at 02:00
  • Do you know if there is a way to filter or find an element from end() or .prevObject, but still maintain the javascript? Both those functions work, but return the full page. – fanfavorite May 17 '11 at 14:35