Execute Javascript after page is fully loaded!

Hi,

i use Vaadin14 and i want implement a scrolling function after my page is fully loaded.
I have written a simple JsModule (@JsModule(“./src/scroll.js”)) that does the scrolling part:

window.customscroll = function (key) {
    var elementById = document.getElementById(key);
        elementById.scrollIntoView({behavior: 'smooth'});
}

and i execute this code in the afterNavigation Method with the following code:

UI.getCurrent().getPage().executeJs("customscroll($0);", key);

If the side is fully loaded the scrolling works but i want that the scrolling is also functional after the first page loading.
I want for example enter the url abc.com/link/to/my/element and after the side is loaded the page should scroll to the key (“link/to/my/element”). But thats not working. The only solution that worked was with a delay:

 setTimeout(function(){
        var elementById = document.getElementById(key);
        elementById.scrollIntoView({behavior: 'smooth'});
    }, 1000);

but i don’t want to set a delay, i use this function also for my normal menu navigation.
I have also read in older posts that i should call my Js-Code in the onAttach Method but that did also not work for me.