Tuesday, September 30, 2008

[jQuery] Re: stripping style from ajax html

Hi Prajwala,

Well that kind of worked. I ended up doing this:
data = $(data);
var replace = $('<div></div>');
data.each( function() {
if (
this.tagName != 'STYLE'
&& this.tagName != 'SCRIPT'
&& this.tagName != 'COMMENT'
) {
replace.append( this );
}
} );

I could not get the remove() function to behave as expected. Perhaps
I need to remove all children tags first or something like that?
Anyway this got me the desired results! Thanks!

cRRRum

On Sep 30, 12:47 am, "Prajwala Manchikatla" <m.prajw...@gmail.com>
wrote:
> I tested with this html
> t =
> $('<html><head><style>.sssss{color:red}</style><script>function(){alert("hi")}</script></head><body>ssss</body></html>')
>
> The return value is jquery object with 3 elements.
> when I do t.get(0) it return style tag
> when I do t.get(1) it return script tag
> when I do t.get(2) it return just "ssss" the content of the body tag
>
> So what happened is the jquery did not consider the html, head, body tags.
> It just created style, script and the content of the body tag with the text
> element.
>
> so you can do like this
> t.each(function(){
> if (this.tagName == 'STYLE'){
> $(this).remove();
> return;
> }
> if(this.tagName == 'SCRIPT'){
> $(this).remove();
> }
>
> })
>
> so it will remove the style and script tags.
>
> cheers,
> Prajwala
>
> On Mon, Sep 29, 2008 at 11:24 PM, crrrum <roy....@gmail.com> wrote:
>
> > Hello all,
>
> > I have a webapp displaying email which can sometimes contain HTML
> > message bodies. I use jquery's .ajax method to grab an HTML message
> > body and then put it into a div tag. Problem is if the HMTL contains
> > style or script tags I get unwanted results.
>
> > I've tried $( html ).find( 'style' ).remove(). This will remove the
> > the actual <style> and </style> tags but still leaves the style text
> > itself inside. I tried several variations but I just can't seem to
> > get this to remove the actual style text with the style tags. Any way
> > to do this or will I have to do a bunch of custom search and replaces?
>
> > Thanks in advance,
> > cRRRum

No comments: