JavaScript - Remove everything after <html> tag - Stack Overflow

Can anyone tell me how to write javascript code that removes everything after the html tag.I have the f

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.

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.

Share Improve this question asked Mar 25, 2010 at 15:55 šljakeršljaker 7,38415 gold badges47 silver badges81 bronze badges 5
  • Why would you have anything outside of HTML tag? – ant Commented Mar 25, 2010 at 16:14
  • Why are you doing this? Are you trying to remove the ads that places like geocities put on your free website? – davr Commented Mar 25, 2010 at 16:15
  • 1 @šljaker maybe you read somewhere that script tags should be at the end of the file, that is true but just before </html> tag – ant Commented Mar 25, 2010 at 16:15
  • I found how to do it I just need to refine my code first. – Jonathan Czitkovics Commented Mar 25, 2010 at 16:28
  • I have your answer posted now. – Jonathan Czitkovics Commented Mar 25, 2010 at 16:48
Add a ment  | 

4 Answers 4

Reset to default 3

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 ment by adding the closing tag:

</html><!--<script>function tracker(){In Here}</script>-->

The tracker script just bees a ment 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 ment tag like this:

</html><div id='deleteThis'><!--<script>function tracker(){In Here}</script>

The browser changes it by closing the ment 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745391726a4625691.html

相关推荐

  • JavaScript - Remove everything after &lt;html&gt; tag - Stack Overflow

    Can anyone tell me how to write javascript code that removes everything after the html tag.I have the f

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信