{"id":83,"date":"2025-08-02T14:43:54","date_gmt":"2025-08-02T14:43:54","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=83"},"modified":"2025-08-02T14:43:54","modified_gmt":"2025-08-02T14:43:54","slug":"incompetent-browsers-towards-a-complete-modern-solution-part-ii","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-towards-a-complete-modern-solution-part-ii\/","title":{"rendered":"Incompetent Browsers: towards a complete, modern solution, part II"},"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<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/\">Requirements and Loaders<\/a><\/p>\n<h3>Detection<\/h3>\n<p>Our basic logic is:<\/p>\n<pre>\r\n<code>\r\nif (browserIsGood) {\r\n  loadJavascriptAndCSS();\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Let&#8217;s dig in to what sits behind that <tt>browserIsGood<\/tt> calculation. We&#8217;re currently using module support as our test, so a simple version would be:<\/p>\n<pre>\r\n<code>\r\nif (HTMLScriptElement.supports('module')) {\r\n  loadJavascriptAndCSS();\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Seems perfectly reasonable. Unfortunately, it&#8217;s not true. <tt>HTMLScriptElement.supports('module')<\/tt> was added much later than actual module support.<\/p>\n<p>We could test for <i>dynamic import<\/i> support. That&#8217;s a lot closer to our real target.<\/p>\n<pre>\r\n<code>\r\nvar test = false;\r\ntry {\r\n  var f = import(\".\/mod.js\").then(\r\n    () => {\r\n      out.innerHTML = \"Module Import Succeeded\";\r\n    },\r\n    () => {\r\n      \/\/ Edge 16 tries to run import, but doesn't actually do so.\r\n      out.innerHTML = \"Module Import Failed\";\r\n    }\r\n  );\r\n  test = true;\r\n} catch (e) {\r\n  test = false;\r\n}\r\n<\/code>\r\n<\/pre>\n<p>But this fails our requirement to be compatible with older browsers! It turns out that <tt>import<\/tt> is not a function! It&#8217;s a reserved keyword and this code will fail with a syntax error in older browsers. But there is a tool we can use to do a syntax check at runtime: <tt>new Function<\/tt>.<\/p>\n<pre>\r\n<code>\r\nvar test = false;\r\ntry {\r\n  var f = new Function('import(\"test dynamic imports\")');\r\n  test = true;\r\n} catch (e) {\r\n  test = false;\r\n}\r\n\r\nvar out = document.getElementById(\"outcome\");\r\n\r\nif (test) {\r\n  out.innerText = \"Test Passed\";\r\n} else {\r\n  out.innerText = \"Test fAILED\";\r\n}\r\n<\/code>\r\n<\/pre>\n<p>Interestingly, we don&#8217;t have to load a real module or deal with the asynchrony. Older browsers will throw an exception with a syntax error, while newer browsers will pass, but the function <tt>f<\/tt> is never actually executed!<\/p>\n<p>This code also fails our requirements to be compatible with older browsers, but in a much smaller window. It fails in IE4, because IE4 doesn&#8217;t support <tt>try<\/tt>. But IE4 will still behave the way we want, because we don&#8217;t want to do anything in IE4, and having our JavaScript not run because of a syntax error is a form of not doing anything.<\/p>\n<h3>Testing the fallback mode<\/h3>\n<p>One of our <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/\">requirements<\/a> is that we be able to enter our fallback mode deliberately for the purpose of testing.<\/p>\n<pre>\r\n<code>\r\nif (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<\/code>\r\n<\/pre>\n<p>This code lets us add <tt>?mode=fallback<\/tt> to our URL and see what it will look like in a browser that doesn&#8217;t pass the test. That&#8217;s useful for developing your fallback experience<\/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, Requirements and Loaders Detection Our basic logic is: if (browserIsGood) { loadJavascriptAndCSS(); } Let&#8217;s dig in to what sits behind that browserIsGood calculation. We&#8217;re currently using module support as our test, so a simple version would be: if (HTMLScriptElement.supports(&#8216;module&#8217;)) { loadJavascriptAndCSS(); } Seems perfectly reasonable. Unfortunately, it&#8217;s not true. HTMLScriptElement.supports(&#8216;module&#8217;) [&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-83","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/83","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=83"}],"version-history":[{"count":6,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/83\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}