views:

46

answers:

3

I have a website on a free domain that has html mark-up added server side to produce web banners. This mark-up is placed above and below the main outer tag. Is it possible to have jQuery strip this out prior to the page rendering?

+1  A: 

Only if your free host allows that (not sure) otherwise you can use the remove method like this:

$('#some_div_id').remove();

Or simply hide() it:

$('#some_div_id').hide();
Sarfraz
Or you can satisfy their impression by letting the banners get included and just hiding them with, `$('#banners').hide()` then they're still in the DOM, but you can't see them.
DavidYell
@DavidYell: Yup, that is another option as well. Thanks
Sarfraz
@sAc: Fair point, i'd not considered it would be breaking terms of use - I'll check that out.
Grant
A: 

Here you go

HTML:

   <div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<html>
    <div>Remove Me</div>
    <body>
        <p>Keep Me</p>
    </body>
    <div>Remove Me</div>
</html>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>
<div>Remove Me</div>

Run This:

$("html div").remove();​

Result is:

<html>
    <body>
        <p>Keep Me</p>
    </body>
</html>​

Test Case: http://jsfiddle.net/RJ6qG/

Revision 1: http://jsfiddle.net/RJ6qG/1/

RobertPitt
I believe Grant meant something like this: `<div>To Be Removed</div><html>…</html><div>To Be Removed</div>`
jigfox
Hi Jens F, you're correct, that's what I meant.
Grant
Does not matter, any items OUTSIDE the html tags are brought inside, and an error is trigger warning you in your log, so this would work for you. the way the dom works is any content on the page gets wrapped inside the html tags before the dom is completed.
RobertPitt
But if you use divs in your page, they will be wiped out: http://jsfiddle.net/RJ6qG/2/
Andir
Ahhh, Im sorry i totally missed that :(, The only way you can do it then is by selector
RobertPitt
+2  A: 

Try:

$("html").siblings().hide();

or

$("html").siblings().remove();

(Edit: This isn't working... should have tested it before posting)

However:

$("div:last").hide();

Will hide the div in this instance:

<html>
<body>
    <div>test inside</div>
</body>
</html>
<div>test outside</div>

So you can get access to them, but it may be a little flaky.

Andir
The original is most likely not working because both IE and Firefox I tested move the div into the body where it should be. You can do simple selectors like :first and :last to get access to them.
Andir