{"id":65,"date":"2025-07-30T15:39:53","date_gmt":"2025-07-30T15:39:53","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=65"},"modified":"2025-08-03T17:01:31","modified_gmt":"2025-08-03T17:01:31","slug":"incompetent-browsers-towards-a-complete-modern-solution-part-i","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/","title":{"rendered":"Incompetent Browsers: towards a complete, modern solution, part I"},"content":{"rendered":"<p>In Part One, we looked at the <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-grading\/\">historical practice of browser grading<\/a>. In Part Two, we looked at some <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-detection\/\">historical solutions for browser detection<\/a>. Today, we&#8217;re going to start putting it together.<\/p>\n<h3>Requirements<\/h3>\n<p>Let&#8217;s start by thinking about browser support. A lot of our historical solutions came from a world where ongoing support for IE was an issue; thank goodness we don&#8217;t live there anymore. Our biggest danger these days is someone coming to our site with an old phone that has a several years old version of webkit or chromium. Especially if they are using an app-hosted browser that uses a system webview.<\/p>\n<p>There are 3 browser engines commonly in use, and 3 obsolete browser engines. These are our browser grades:<\/p>\n<p><strong>A-grade experience<\/strong>: If someone comes to the site with an up-to-date browser running Gecko, Blink, or Webkit, we want to give them the full experience.<\/p>\n<p><strong>Obsolete C-grade experience<\/strong>: If someone comes to the site with an out-of-date browser running Gecko, Blink, or Webkit, we want to give them the plain HTML experience.<\/p>\n<p><strong>Archaic C-grade experience<\/strong>: If someone comes to the site with a browser running Trident (IE,) Spartan (Old Edge,) Presto (old Opera,) or some other ancient thing (Netscape? iCab? Lynx?) we want to give them the plain HTML experience.<\/p>\n<p>It&#8217;s probably worth the time to add a few more functional requirements:<\/p>\n<p><strong>Opt-In<\/strong>: We&#8217;ll have to develop a plain HTML experience for our site. It would be nice to be able to develop this in a modern browser, so we should be able to opt-in to the plain HTML experience for development.<\/p>\n<p><strong>ES3\/Jscript Compatible<\/strong>: If we write any code that decides if we should load our JavaScript and CSS, that code will have to run successfully in any browser. Not just any modern browser; <strong>any<\/strong> browser. This means limiting ourselves to EcmaScript version 3, and also to the limitations of the not-quite compatible JScript. This code will be written <a href=\"https:\/\/www.youtube.com\/watch?v=rblt2EtFfC4\" rel=\"noopener\" target=\"_blank\">like it&#8217;s 1999<\/a>.<\/p>\n<h3>Let&#8217;s write some code<\/h3>\n<p>The simplest answer is to load our full experience with a module tag, and let it load everything.<\/p>\n<p><code><br \/>\n&lt;script type=\"module\" src=\".\/main.js\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>And maybe you can get away with that. But this is likely to leave you with a <a href=\"https:\/\/stackoverflow.com\/questions\/3221561\/eliminate-flash-of-unstyled-content\" rel=\"noopener\" target=\"_blank\">Flash of Unstyled Content<\/a>, as your CSS won&#8217;t be loaded until your JavaScript runs.<\/p>\n<p>We really want something like this:<\/p>\n<pre>\r\n<code>\r\nif (browserIsGood) {\r\n  loadJavascriptAndCSS();\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Okay, that&#8217;s a simple structure, but there&#8217;s a lot of handwaving happening there. Let&#8217;s dig in.<\/p>\n<h3>Loading JavaScript<\/h3>\n<p>Can we just use dynamic import?<\/p>\n<pre>\r\n<code>\r\nif (browserIsGood) {\r\n  import(\".\/main.js\");\r\n}\r\n<\/code>\r\n<\/pre>\n<p>No, we can not! <tt>import<\/tt> is a reserved keyword in JavaScript. This will crash if you try to run it in a browser that doesn&#8217;t support dynamic import. We&#8217;ll have to load it the old fashioned way.<\/p>\n<p>Everyone knows how to do this by now, right?<\/p>\n<pre>\r\n<code>\r\nvar target = document.getElementsByTagName(\"script\")[0];\r\nvar head = target.parentNode;\r\n\r\nfunction insertScript(src) {\r\n  var s = document.createElement(\"script\");\r\n  s.setAttribute(\"src\", src);\r\n  s.setAttribute(\"type\", \"module\");\r\n  head.insertBefore(s, target);\r\n}\r\n\r\nif (browserIsGood) {\r\n  insertScript(\".\/main.js\");\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Here we use the first script tag as the target for inserting, because the page might not have a <tt>head<\/tt> tag, and we create a script tag dynamically. Our JavaScript will be loaded asynchronously, so we&#8217;ll need to wait for <tt>DOMContentLoaded<\/tt> before we do anything. But these are table stakes for a modern web site these days. No big deal.<\/p>\n<h3 id=\"loading-css\">Loading CSS<\/h3>\n<p>So how can we load CSS dynamically without causing a FOUC?<\/p>\n<pre>\r\n<code>\r\nvar target = document.getElementsByTagName(\"script\")[0];\r\nvar head = target.parentNode;\r\n\r\nfunction insertStyle(src) {\r\n  document.write('<link rel=\"stylesheet\" href=\"' + src + '\">');\r\n}\r\n\r\nif (browserIsGood) {\r\n  insertStyle(\".\/main.css\");\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Wait, really? <tt>document.write<\/tt>? Yes. I&#8217;m sorry, but if you want to prevent a FOUC, you need there to be a <tt>link<\/tt> tag in the <tt>head<\/tt> before the parser sees the <tt>body<\/tt>. Browsers will block rendering in that case, and will not block rendering, leading to a FOUC in every other case.<\/p>\n<p>Unless you want to get a little crazy and do it all yourself.<\/p>\n<pre>\r\n<code>\r\nvar target = document.getElementsByTagName(\"script\")[0];\r\nvar head = target.parentNode;\r\n\r\nvar hide = document.createElement(\"style\");\r\n\r\nfunction hideDocument() {\r\n  hide.innerText = \"body {display:none;}\";\r\n  head.insertBefore(hide, target);\r\n}\r\n\r\nfunction handler() {\r\n  head.removeChild(hide);\r\n}\r\n\r\nfunction insertStyle(src) {\r\n  var ss = document.createElement(\"link\");\r\n  ss.setAttribute(\"rel\", \"stylesheet\");\r\n  ss.setAttribute(\"href\", src);\r\n\r\n  if (ss.addEventListener) {\r\n    ss.addEventListener(\"load\", handler, false);\r\n    ss.addEventListener(\"error\", handler, false);\r\n    waiting[ss.href] = true;\r\n  }\r\n\r\n  head.insertBefore(ss, target);\r\n}\r\n\r\nif (browserIsGood) {\r\n  hideDocument();\r\n  insertStyle(\".\/main.css\");\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Here we add a <tt>style<\/tt> tag that hides the <tt>body<\/tt> tag, and then we remove it once the stylesheet loads (or fails to load). Note that we only call <tt>hideDocument<\/tt> once we know we&#8217;re inside a good browser.<\/p>\n<link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/styles\/default.min.css\">\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/highlight.min.js\"><\/script><br \/>\n<script>hljs.highlightAll();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part One, we looked at the historical practice of browser grading. In Part Two, we looked at some historical solutions for browser detection. Today, we&#8217;re going to start putting it together. Requirements Let&#8217;s start by thinking about browser support. A lot of our historical solutions came from a world where ongoing support for IE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":13,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/65\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}