简体   繁体   English

在HTML文件和外部脚本文件中两次使用onload的Javascript

[英]Javascript using onload twice in html file and external script file

I have an external JS file that I load into my HTML 我有一个外部JS文件,已加载到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标签中还有一个onload函数,这是新功能,可以帮助我在页面加载时专注于用户名文本字段。

<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. 但是,由于我添加了最后一项功能,因此忙碌指示器停止工作,因为它也使用了onload函数。 What can I do to make it work again? 我该怎么做才能使其再次正常工作?

这不是最优雅的方法..但它可以工作

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

Remove your body onload tag and put: 删除您的身体onload标签并放入:

<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). 在您的外部Java脚本之后(确保它仍在<head>标记中)。 That should change window.onLoad to your custom function, which does what you need and then calls setupFunc. 这应该将window.onLoad更改为您的自定义函数,该函数将执行您需要的操作,然后调用setupFunc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM