Skip to content

Commit

Permalink
fix(core feature-detection): Fix loading of modernizr script.
Browse files Browse the repository at this point in the history
In some situations loading of the modernizr.min.js script failed because
the base url could be incorrect. That is fixed with a non-IE compatible
method of getting the current's script URL, which is safe to use.
The problem surfaced in Plone while loading the Patternslib library and
in between the protect.js script was loaded. The base url was calculated
for the protect.js script and the modernizr script could not be loaded.
  • Loading branch information
thet committed Jan 28, 2025
1 parent a3b6f48 commit 89141ae
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/core/feature-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
}

// Get the current script tag's URL.
// See: https://stackoverflow.com/a/984656/1337474
const scripts = document.getElementsByTagName("script");
const script = scripts[scripts.length - 1];
let script_url = script.src;
const script_url = document.currentScript.src;
// Get the base URL of the current script tag's URL.
script_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
let base_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
base_url = base_url.replace("chunks/", "");

// Inject a new one with the modernizr bundle.
const script_tag = document.createElement("script");
script_tag.src = script_url + "modernizr.min.js";
script_tag.src = base_url + "modernizr.min.js";
document.getElementsByTagName("head")[0].appendChild(script_tag);
})();

0 comments on commit 89141ae

Please sign in to comment.