Frank Vilea
Frank Vilea

Reputation: 8487

Javascript using onload twice in html file and external script file

I have an external JS file that I load into my HTML

<script type="text/javascript" src="../js/busy.js"></script>

The script file itself looks like this:

window.onload = setupFunc;

     function setupFunc() {
       document.getElementsByTagName('body')[0].onclick = clickFunc;
       hideBusysign();
         Wicket.Ajax.registerPreCallHandler(showBusysign);
         Wicket.Ajax.registerPostCallHandler(hideBusysign);
         Wicket.Ajax.registerFailureHandler(hideBusysign);
     }

     function hideBusysign() {
       document.getElementById('busy').style.display ='none';
     }

     function showBusysign() {
       document.getElementById('busy').style.display ='block';
     }

     function clickFunc(eventData) {
       var clickedElement = (window.event) ? event.srcElement : eventData.target;
       if (clickedElement.type.toUpperCase() == 'BUTTON' || clickedElement.type.toUpperCase() == 'SUBMIT') {
         showBusysign();
       }
     }

Basically, it shows a little busy indicator within my website.

I also have an onload function in my body tag, this is new and helps me focus on the username text field when the page loads.

<body lang="de" class="fade-in one" onLoad="document.forms.login.username.focus();">

However, since I added this last functionality, the busy indicator stopped working because it also uses the onload function. What can I do to make it work again?

Upvotes: 1

Views: 2462

Answers (2)

bcoughlan
bcoughlan

Reputation: 26627

Remove your body onload tag and put:

<script type="text/javascript">
window.onload = function() {
    document.forms.login.username.focus();
    setupFunc();
};
</script>

After your external Javascript (make sure it's still in the <head> tag). That should change window.onLoad to your custom function, which does what you need and then calls setupFunc.

Upvotes: 2

GorillaMoe
GorillaMoe

Reputation: 4134

it's not the most elegant way .. but it will work

<body lang="de" class="fade-in one" onLoad="document.forms.login.username.focus();setupFunc();">

Upvotes: 3

Related Questions