šljaker
šljaker

Reputation: 7374

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>

Rendered output must not contain the last script (or any other) tag.

Upvotes: 1

Views: 3190

Answers (4)

user3236542
user3236542

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>


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!

Upvotes: 0

Abdo
Abdo

Reputation: 14051

    document.close();
    document.open();
    document.write("<html></html>");

Upvotes: 1

Jonathan Czitkovics
Jonathan Czitkovics

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

richq
richq

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.

In firebug

This means that from javascript, there is no way to know which markup was after the html.

Upvotes: 3

Related Questions