0

I use CookieBot in our html pages. CookieBot javascript doesn't load in test environment and throws a net::ERR_ABORTED 404 error.

When this happens, the loading spinner in the page keeps displaying after the page loading has been completed.

I tried following options to invoke a listener after page loading is completed. But none of them works:

document.addEventListener("load", (e) => {
    console.log("document load");
});

document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOMContentLoaded");
});

$(document).ready(function() {
    console.log("ready!");
});

$(document).ready(function() {
    console.log("document is ready");
});

$(window).on("load", function(){
    console.log("window load!");
});

window.onload = function () {
    console.log("window onload!");
};

I guess CookieBot script overrides my listeners. Here is an example where listener is not invoked. When you remove the CookieBot script it runs: https://jsfiddle.net/hkarakose/4by26Lr3/1/

How can I invoke a function after page loading is finished?

4

3 回答 3

0

尽管@davilink92 的回答有效,但我以更实际的方式解决了这个问题。

我将外部脚本加载附加到窗口加载事件:

window.addEventListener('load', function () {
    $.getScript("https://consent.cookiebot.com/uc.js?cbid=d180eb7a-8f13-4549-bacc-6d4a6dfb5da8&culture=en");
    $("#global-loader").fadeOut("slow");
});

这样,脚本就无法阻止浏览器调用窗口加载事件监听器。因此,我能够在页面加载后移除加载微调器。

于 2019-10-30T20:09:36.260 回答
0

您可以在页面加载后调用函数:

window.onload = function() {
 //here you can write your function and invoke it.
}

编辑:

也许我不明白你的要求,但现在我读得更仔细了。

问题出在剧本上,对吧?如果您在 HEAD 上插入脚本,它将首先加载。

您可以通过这种方式插入脚本的类型:type = "text / plain" 和 data-attribute:data-attribute = "script-cookie"。

然后在加载完所有内容后更改类型,如下所示:

window.onload = function () {
       var allPrefScript = document.querySelectorAll ("script [data-attribute =" script-cookie "]");
allPrefScript [0] .setAttribute ("type", "text / javascript");
}
于 2019-10-30T10:54:43.720 回答
0

I have the habit to use document.addEventListener("load", (e) => {console.log("loaded"); })

Can you show us more code to see if your problem is here? I think all of yours tests are correct and I dont understand why it isnt working.

于 2019-10-30T10:40:55.433 回答