1

I'm writing a small script that determines if the user is on IE8 or below. If they are, the script should completely empty the document (body and head) and stop any further script executing.

I've played around with document.write() but can only get this working with window.onload. But I want it to execute as soon as it knows the browser version (which is when the script executes).

Example page setup:

<html>
  <header>
    Some CSS
    Some meta
    ...
  </head>
  <body>
    Page content

    <script>
      if (IE < 8) { //in reality I have a function to determine this
        document.write('You browser is outdate. Please upgrade to view this site.');
      }
    </script>

    <script src="more-scripts"></script>
  </body>
</html> 

This doesn't work but if I wrap the script in a window.onload it does. But then the page flashes up before the code executes. How can I get this to work?

CaribouCode
  • 12,129
  • 20
  • 75
  • 149
  • 2
    There are other well-refined ways of doing this. You'll find them by seraching. – Popnoodles Sep 09 '14 at 15:41
  • You should post the exact code you have when talking about [`document.write()`](https://developer.mozilla.org/en-US/docs/Web/API/document.write). "Writing to a document that has already loaded without calling [`document.open()`](https://developer.mozilla.org/en-US/docs/Web/API/document.open) will automatically perform a document.open." – Teemu Sep 09 '14 at 15:45
  • @Popnoodles I did search and couldn't find any solutions that work for me. Why else do you think I've posted here? Your comment is not constructive. – CaribouCode Sep 09 '14 at 15:45
  • Sigh, are people still doing these "upgrade" messages? – Bart Sep 09 '14 at 15:46
  • @Bart I don't really fancy supporting the morons still using IE6. – CaribouCode Sep 09 '14 at 15:48
  • 1
    @Cooper what makes you think IE6 users (1) have admin rights to upgrade their work computer or (2) know how to upgrade in the first place? The latter group probably doesn't even know what this "internet explorer" you're talking about is. Not everyone is an IT'er. Also, why don't you just redirect the page to an error page? – Bart Sep 09 '14 at 15:51
  • @Bart I'd rather they didn't see the site at all than see it looking broken and disgusting. If that means they get a message like this that they don't know what to do with, or can't upgrade then so be it. Like I said, it's a better option. – CaribouCode Sep 09 '14 at 15:56
  • Use [window.stop](http://stackoverflow.com/a/10415265/2464634) after `document.write`. This is a duplicate question of: http://stackoverflow.com/questions/8943219/how-to-stop-page-load-in-html-static-page – Preston S Sep 09 '14 at 15:56
  • @PrestonS Thanks, I didn't know about that code. But I think you mean `document.execCommand('Stop');` in this case. – CaribouCode Sep 09 '14 at 16:02
  • My comment would be constructive for someone who hadn't searched already. You didn't specify that you had... – Popnoodles Sep 09 '14 at 17:09
  • @Popnoodles You should give people the benefit of the doubt that they've at least searched and tried something before coming to this forum. – CaribouCode Sep 09 '14 at 19:16
  • @Cooper welcome to Stack Overflow. – Popnoodles Sep 09 '14 at 19:39

2 Answers2

1

Rather than using document.write() to print a message, you can use the .innerHTML property of the document.body element to entirely replace the body of the page. For this technique, your browser-check script should go in the head section, not the body (this is usually where scripts like this would go anyway).

<html>
  <header>
    Some CSS
    Some meta
    ...
    <script>
      if (IE < 8) { //in reality I have a function to determine this
        document.body.innerHTML = "You browser is outdate. Please upgrade to view this site.";
      }
    </script>
  </head>
  <body>
    Page content
    <script src="more-scripts"></script>
  </body>
</html> 
Woodrow Barlow
  • 6,567
  • 2
  • 33
  • 76
  • let me know if it doesn't work. i don't have the tools to test it at the moment. – Woodrow Barlow Sep 09 '14 at 15:47
  • It's not working. I'm testing it in Chrome without the if statement, so the script should execute everytime. the `document.write(' – CaribouCode Sep 09 '14 at 15:50
  • @Cooper i've updated with a new technique. here is a proof of concept: http://jsfiddle.net/3vuftj0c/ – Woodrow Barlow Sep 09 '14 at 15:56
  • Cool I've got it working now, I also had to add `document.head.innerHTML = "";` Thanks for your help. – CaribouCode Sep 09 '14 at 15:59
  • it's worth noting that there are a lot of arguments against using the `document.write()` function, except in debugging (though it's not "wrong", per se). [here](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) is a related SO question that discusses that. – Woodrow Barlow Sep 09 '14 at 15:59
  • This does not address stopping execution of JavaScript on the page. – Preston S Sep 09 '14 at 16:05
  • @PrestonS do you mean that it doesn't terminate javascript that is in the middle of execution, or that it doesn't prevent scripts that would be loaded later on from loading? i interpreted the question as asking for the latter. – Woodrow Barlow Sep 09 '14 at 17:04
  • @WoodrowBarlow For instance it would not prevent the execution of an onload event listener or other such window events that were attached in the `` before your script is run. `window.stop()` or `document.execCommand('stop')` would. – Preston S Sep 09 '14 at 18:05
0

you could use conditional comments for that:

<!--[if IE 8]>
  <script>
        document.body.innerHTML = '';
        document.write('You browser is outdate. Please upgrade to view this site.');
    </script>
<![endif]-->
shershen
  • 9,496
  • 10
  • 36
  • 55
  • And this script does this: "Empty document and stop further javascript" ? – Popnoodles Sep 09 '14 at 15:42
  • updated a bit, since we're removing all from it should – shershen Sep 09 '14 at 15:44
  • This doesn't answer my question AT ALL. Getting the script to work for the correct versions of IE was not the problem. The script doesn't do the job I want it do it which is clear the document and make sure any script after it doesn't execute. – CaribouCode Sep 09 '14 at 15:44