{"id":97,"date":"2025-08-04T15:21:42","date_gmt":"2025-08-04T15:21:42","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=97"},"modified":"2025-08-04T15:21:43","modified_gmt":"2025-08-04T15:21:43","slug":"incompetent-browsers-a-complete-modern-solution","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-a-complete-modern-solution\/","title":{"rendered":"Incompetent Browsers: a complete, modern solution"},"content":{"rendered":"<p>Previously: <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-grading\/\">Browser Grading<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-detection\/\">Historical Browser Detection<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/\">Requirements and Loaders<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-towards-a-complete-modern-solution-part-ii\/\">Detection and Fallback<\/a>.<\/p>\n<h3>Code we can be reasonably happy with<\/h3>\n<p>Here&#8217;s a complete detector\/loader:<\/p>\n<pre>\r\n<code>\r\n&lt;link rel=\"preload\" as=\"style\" href=\"\/styles\/core.css\" \/&gt;\r\n&lt;link rel=\"preload\" as=\"style\" href=\"\/styles\/structure.css\" \/&gt;\r\n&lt;link rel=\"preload\" as=\"style\" href=\"\/styles\/content.css\" \/&gt;\r\n&lt;link rel=\"preload\" as=\"script\" href=\"\/scripts\/bundle.js\" \/&gt;\r\n\r\n&lt;script&gt;\r\n  (function (mainStyles, mainScripts, fallbackStyles, fallbackScripts) {\r\n    var target = document.getElementsByTagName('script')[0];\r\n    var head = target.parentNode;\r\n    var mode;\r\n\r\n    function insertStyle(src) {\r\n      document.write('&lt;link rel=\"stylesheet\" href=\"' + src + '\"&gt;');\r\n    }\r\n\r\n    function insertScript(src) {\r\n      var s = document.createElement('script');\r\n      s.setAttribute('src', src);\r\n      head.insertBefore(s, target);\r\n    }\r\n\r\n    try {\r\n      if (window.URL && window.URLSearchParams) {\r\n        var url = new URL(document.URL);\r\n        mode = url.searchParams.get('mode');\r\n        if (mode == 'fallback') {\r\n          throw new Error('fallback mode requested');\r\n        }\r\n      }\r\n      if (mode != 'broken') {\r\n        var f = new Function('import(\"test dynamic imports\")');\r\n        for (var i = 0; i &lt; mainStyles.length; i++) {\r\n          var ss = mainStyles[i];\r\n          insertStyle(ss);\r\n        }\r\n        for (var i = 0; i &lt; mainScripts.length; i++) {\r\n          var s = mainScripts[i];\r\n          insertScript(s);\r\n        }\r\n      }\r\n    } catch (e) {\r\n      for (var i = 0; i &lt; fallbackStyles.length; i++) {\r\n        var ss = fallbackStyles[i];\r\n        insertStyle(ss);\r\n      }\r\n      for (var i = 0; i &lt; fallbackScripts.length; i++) {\r\n        var s = fallbackScripts[i];\r\n        insertScript(s);\r\n      }\r\n    }\r\n  })(\r\n    ['\/styles\/core.css', '\/styles\/structure.css', '\/styles\/content.css'],\r\n    ['\/scripts\/bundle.js'],\r\n    ['\/styles\/fallback.css'],\r\n    ['\/scripts\/fallback.js']\r\n    \/* Don't autoformat this file. If the formatter adds a trailing comma it will break IE *\/\r\n  );\r\n&lt;\/script&gt;\r\n&lt;script&gt;\/* We need this script to prevent a FOUC *\/&lt;\/script&gt;\r\n<\/code>\r\n<\/pre>\n<p>What&#8217;s new in this version? Well, it&#8217;s wrapped in a function expression so it&#8217;s not making a bunch of global variables. It can load multiple scripts and styles. It can load scripts and styles in the fallback case. (Just make sure they&#8217;re written to work in Netscape 4!)<\/p>\n<p>Because we&#8217;re allowing scripts and styles in the fallback path, we also support <tt>?mode=broken<\/tt>, which loads nothing at all, so you can check how it will look both IE9 and IE3. (IMO, this is excessive, and I wouldn&#8217;t allow fallback scripts or styles if I ran the zoo.)<\/p>\n<p>We&#8217;ve also included <tt>preload<\/tt> tags for the files that competent browsers want. That means they&#8217;ll start downloading right away. This is straight out of a project I was working on which built to a plain script; If your JavaScript is served as a module, make sure to use <tt>modulepreload<\/tt> for your module instead.<\/p>\n<p>Note also the two comments near the end. A lot of code formatters like to leave trailing commas on parameter lists, which helps make diffs more readable. This is totally legal in JavaScript, but not in Microsoft JScript. You&#8217;ll get a syntax error in IE if you do that.<\/p>\n<p>The other thing is the empty <tt>&lt;\/script&gt;<\/tt> tag at the end. It turns out that Firefox only blocks rendering and waits for stylesheets if you do this. It doesn&#8217;t have to be empty, which is why you almost never see it these days, but when you <tt>document.write()<\/tt> your stylesheet, there needs to be some script tag to trigger layout blocking.<\/p>\n<p>I spent a lot of time debugging this guy in BrowserStack.<\/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>Previously: Browser Grading, Historical Browser Detection, Requirements and Loaders, Detection and Fallback. Code we can be reasonably happy with Here&#8217;s a complete detector\/loader: &lt;link rel=&#8221;preload&#8221; as=&#8221;style&#8221; href=&#8221;\/styles\/core.css&#8221; \/&gt; &lt;link rel=&#8221;preload&#8221; as=&#8221;style&#8221; href=&#8221;\/styles\/structure.css&#8221; \/&gt; &lt;link rel=&#8221;preload&#8221; as=&#8221;style&#8221; href=&#8221;\/styles\/content.css&#8221; \/&gt; &lt;link rel=&#8221;preload&#8221; as=&#8221;script&#8221; href=&#8221;\/scripts\/bundle.js&#8221; \/&gt; &lt;script&gt; (function (mainStyles, mainScripts, fallbackStyles, fallbackScripts) { var target = document.getElementsByTagName(&#8216;script&#8217;)[0]; var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/97","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=97"}],"version-history":[{"count":8,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/97\/revisions\/105"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}