How do you perform a javascript function on a ui page onload?

mahudg2
Kilo Expert

What I want is pretty simple. I just want a javascript function to execute as soon as the page loads. I'm attempting to get this to work in a ui page.

 

This is how I would normally perform this

 

<body onload="myFunction()">

 

and then in the client script I would have the function

function myFunction(){

        alert("Yay! This function executed on load!");

}

 

However in service now I get no such luck. Is there anyway I can perform this?

2 REPLIES 2

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I usually use the prototype method document.observe in jelly pages in ServiceNow. You can do:



document.observe("dom:loaded", function() {


  //dostuff


  alert("Yay! This function executed on load!");


});


adiddigi
Tera Guru

While there is nothing wrong with what you are doing, if you want to do it by using Service Now's API methods, you will have to make a   call to a   function called addLoadEvent(); which takes a function as a parameter.



example : addLoadEvent(loadMe);



function loadMe(){


alert('Loading');


}



Hope that helps!