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>
1
  • Do you mean Javascript as in a <script> tag? That should work... Commented May 16, 2011 at 22:38

4 Answers 4

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good job. Didn't think in this way. Genius, I should say.
0

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

1 Comment

.load initiates an ajax request. The questioner is trying to return the contents of a tag, not get more contents from the server.
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.

1 Comment

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.
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).

3 Comments

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?
prevObject is not a plugin. james.padolsey.com/javascript/…, You are right, it returns the whole page.(I was testing only with the script and the html part within the content div)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.