Unexplained html() and context behavior

Unexplained html() and context behavior

So I'm trying to work on some functions that use $.ajax() and then [hopefully] strip out <script> elements from the response data and execute them manually. But I'm unable to select the <script> elements even though I can see them inside of the response data that $.ajax() is receiving.

So I came up with these tests to see if I'm sane, and I was wondering if anyone could explain to me why they evaluate as they do. (I'm executing these statements in Safari 5's Javascript debugger)

If I execute these statements on the www.jquery.com home page, I get:
  1. $('*').length;
  2. >>>> 253
  3. $('*', $('*').html()).length;
  4. >>>> 235
  6. $('script').length;
  7. >>>> 8
  8. $('script', $('*').html()).length;
  9. >>>> 0
I also find this very odd:
  1. $('body').length;
  2. >>>> 1
  3. $('body', $('*').html()).length;
  4. >>>> 0
So what I'm confused about is that when executing a selector on the specified context, it seems to be missing elements. I have verified that the script elements are in the text returned by $('*').html(). So if the text returned by $('*').html() has script elements and has a body element, shouldn't using that text as a context for a selector return those elements?

Any help would be greatly appreciated.
    • Topic Participants

    • jon