javascript on-load in HTML snippet

4
Hi all, I have seen this question pop-up several times and I have never seen a good, generic answer for this. This can be just me and I hope it is, but it keeps bugging me: How can I trigger javascript code after page load via the HTML snippet? As a sidenote, I need the DOM to be fully loaded, hence want to make use of the 'after page load' events. mx.addonload / $(document).ready etc. are not working for this, since Mendix and specifically the dojo library seem to block the pageload events for being called on. Now I am always building Custom Widgets for this, but this doesn't make sense in my opinion. Any help on this would be welcome!
asked
2 answers
0

Here is a solution with modern-day technology called jQuery.

Ever since the html-snippet has the option ‘JavaScript with jQuery’, you have the option to listen to the ‘document loaded’-event. See this code bit that sets focus to an input element that has classname ‘focusonme’:

$( document ).ready(function() {
let narcissus=$('.focusonme')[0]; //Obviously: add class 'focusonme' to the desired input
if (typeof narcissus !='undefined'){
    let myinput = narcissus.firstChild; 
    myinput.focus();
}});

answered
0

Hi Ivo,

I’ve done functionalities like this in the htmlsnippet, and you should set the eventlistener to the window

window.mx.addOnLoad(function() {
    myFunction()
});

function myFunction(){
   // do something here
}

Hope this helps

answered