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
2010-07-15 13:42:34
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
2010-07-15 13:43:44
@DavidYell: Yup, that is another option as well. Thanks
Sarfraz
2010-07-15 13:44:41
@sAc: Fair point, i'd not considered it would be breaking terms of use - I'll check that out.
Grant
2010-07-15 14:21:02
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
2010-07-15 13:51:50
I believe Grant meant something like this: `<div>To Be Removed</div><html>…</html><div>To Be Removed</div>`
jigfox
2010-07-15 13:53:48
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
2010-07-15 14:49:02
But if you use divs in your page, they will be wiped out: http://jsfiddle.net/RJ6qG/2/
Andir
2010-07-15 21:03:13
Ahhh, Im sorry i totally missed that :(, The only way you can do it then is by selector
RobertPitt
2010-07-15 22:30:17
+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
2010-07-15 13:56:19
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
2010-07-15 14:33:04