- 347 views
The workaround
You should avoid writing code in window.load or document.ready function when writing javascript in Drupal 8. I stumbled across this issue thread on Drupal.org and it works like a charm!
Example code
This code should run only once, just place it in your theme javascript file and you should be fine.
var checkReadyState = setInterval(() => {
if (document.readyState === "complete") {
clearInterval(checkReadyState);
console.log('Page is loaded.');
/* Put your code logic here */
(function ($){
/* Put jQuery code logic here */
})(jQuery);
}
}, 100);
Fixed!
Now you should be ready to go. Rock some Drupal 8 then!