JavaScript - Remove everything after </html> tag
Can anyone tell me how to write javascript code that removes everything after the html tag.
I have the following file:
<html>
<head>
<script>
// my script
</script>
</head>
<body>
Some text
</body>
</html>
<script>
</script>
Re开发者_高级运维ndered output must not contain the last script (or any other) tag.
This code waits for the page to load and then removes all tags after the </html>
try it with firebug and you will see. You need to create a place holder as such in this example <div id="last"></div>
after your last tag and then it removes the rest of the tags.
<html>
<head>
<script type="text/javascript">
window.onload=function()
{
var body=document.getElementsByTagName('body')[0];
var found=false;
var cur=0;
for(var i=0; i<body.childNodes.length; i++)
{
if(body.childNodes[i].id=='last')
{
cur=i;
found=true;
}
else if(found && i>cur+1)
{
body.removeChild(body.childNodes[i]);
}
}
}
</script>
</head>
<body>
Some text
<div id="last">
</div>
</body>
</html>
<p>
Im not gonna show
</p>
<input/>
<script>
</script>
<b>
</b>
Don't forget to scroll! Enjoy!
Oddly enought the formating of code here is not functioning right. Its probably my fault.
Edit renamed my variable to body
The problem is that in the DOM, outside of HTML there is nothing. Your closing </html>
gets moved to account for the badly formatted HTML. Here is how firebug shows your example.
This means that from javascript, there is no way to know which markup was after the html.
document.close();
document.open();
document.write("<html></html>");
Trackers are usually added during the transfer to the client-side by the website server.
They place their code like this:
</html><script>(function tracker(){In Here})</script>
The browser changes it to this:
<script>(function tracker(){In Here})</script>
</body>
</html>
So if you just put <!-- after the </html> so it looks like this, shown below with the tracker added
</html><!--<script>(function tracker(){In Here})</script>
The browser will changes it all to a comment by adding the closing tag:
</html><!--<script>function tracker(){In Here}</script>-->
The tracker script just becomes a comment and stay's outside the html where it will not function!
This is the reason why some code can be after the website's last tag!
Why block it? 1 tracker can bring in hundreds if they want! All on the client's-side internet connection! Where your code gets blamed for the third party actions!
If you want to use Javascript to delete the code put an opening tag for a div and give it an ID also add the opening comment tag like this:
</html><div id='deleteThis'><!--<script>function tracker(){In Here}</script>
The browser changes it by closing the comment and div tags:
<div id='deleteThis'>
<!--<script>(function tracker(){In Here})</script>-->
</div>
</body>
</html>
Tracker-Script should not function but still add in your script-code:
function deleteTracker(){ document.getElementByID('deleteThis').innerHTML="";}
Run as soon as possible, so you should add to the body tag! <body onload='deleteTracker()'>
Everything inside the div will be deleted!
精彩评论