Scrollposition with javascript function in HTML widget

0
Hi All   We are trying to use a javascript for remembering the scroll position of a mendix element in the client and also for setting the same scroll position on that element when the page loads again. The script itself works, but when we want to enter it in the HTML widget and select the option ‘content-type: javascript’ and content:   function savescrollTopState(elementId) {     window.addEventListener('load', function() {        const jqSelector = "." + elementId;       const storageName = elementId + "_scrollTopValue"; console.log(storageName,elementId)       if (localStorage.getItem(storageName) != null) {         document.querySelector(jqSelector).scrollTop = localStorage.getItem(storageName);       }       document.querySelector(jqSelector).addEventListener("scroll", function() {         localStorage.setItem(storageName, document.querySelector(jqSelector).scrollTop);       });     }, false);   }   savescrollTopState("mx-name-dataGrid21");     it does nothing. Are we missing something? Kind regards   Kevin
asked
2 answers
0

Are you getting any errors? Its mostly the case when the DOM has not loaded yet but you tend to write the code to do some DOM Manipulations.

Perhaps you can try to rule out the options by wrapping the code in the Async Block for e.g.: like in a setTimeout function with 0 as delay. (which puts the code in the async queue) so when its time comes the DOM could well be available to get manipulated.

 

 

 

 

answered
0

@Ganesh

 

We were not able to fix it yet but still investigating. Thanks for the tip anyway.

 

Kevin

answered