0

I am trying to stop the body from loading using javascript, and then write something on the body element.

I can do this with:

<html>
<head>
<script>
  document.write('<!--');
  window.onload = function(){
    document.body.innerHTML = "123";
  }
</script>

</head>
<body>
    <script>
        alert(1); // This won't show
    </script>
</body>
</html>

And this works, the alert function will never be called.

But the problem is that if there is another comment tag on the head section, such as:

<html>
<head>
<script>
  document.write('<!--');
  window.onload = function(){
    document.body.innerHTML = "123";
  }
</script>

<!-- other tag -->
</head>
<body>
    <script>
        alert(1); // This will now show.
    </script>
</body>
</html>

How should I handle this? It is very important that all the content on the body element, won't load. Not just hidden, but not loaded.

Afonso Matos
  • 148
  • 3
  • 12
  • 2
    The only way to guarantee that is to not write out the body element to begin with. The browser might have JavaScript disabled for example. – folkol Sep 13 '14 at 14:22
  • 3
    What possible reason could you have for doing this? That's the question you never answered [the last time you asked this](http://stackoverflow.com/questions/25813516/empty-body-element-before-page-renders). – T.J. Crowder Sep 13 '14 at 14:25
  • The duplicate linked from above has been deleted. That said, this should probably be considered a duplicate of https://stackoverflow.com/questions/8943219/how-to-stop-page-load-in-html-static-page. (ping @kapa, any chance you could edit the dupe link to fix it?) – Ilmari Karonen Mar 11 '19 at 19:31

1 Answers1

2

You can try

window.stop()

Demo

Note that's DOM Level 0, not part of any specification. And it isn't supported by IE.

Oriol
  • 274,082
  • 63
  • 437
  • 513