Reputation: 7374
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>
Rendered output must not contain the last script (or any other) tag.
Upvotes: 1
Views: 3190
Reputation: 1
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>
Upvotes: 0
Reputation: 14051
document.close();
document.open();
document.write("<html></html>");
Upvotes: 1
Reputation: 1614
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
Upvotes: 3
Reputation: 56438
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.
Upvotes: 3