Eventually found the answer to my problem

Instead of <body onload=function();>

I added

window.onload=function();

within the <head>......<script language='javascript'></script> after the function is defined.

I know how to add things in the <head> in XWD by simply naming an object '<head>' on the page.