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:
- $('*').length;
- >>>> 253
- $('*', $('*').html()).length;
- >>>> 235
-
- $('script').length;
- >>>> 8
- $('script', $('*').html()).length;
- >>>> 0
I also find this very odd:
- $('body').length;
- >>>> 1
- $('body', $('*').html()).length;
- >>>> 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.