简体   繁体   English

无法启动Javascript加载

[英]Can't get Javascript onload to fire

I have a javascript function for a web site I need to fire as soon as the page loads. 我有一个网站的javascript函数,需要在页面加载后立即启动。

I can actually get the function to fire by attaching it to an event like onkeyup. 我实际上可以通过将函数附加到onkeyup之类的事件来触发该函数。 I just can't get it to fire when the page loads. 当页面加载时,我只是无法启动它。

Another weird thing - it fires when I run the app on my local server - just not when I deploy to the production server. 另一个奇怪的事情-当我在本地服务器上运行应用程序时触发-而不是当我部署到生产服务器时触发。

Here's my function. 这是我的职能。 Like I said it works fine if I have it fire as the result of an onkeyup event - both locally and on the prod server. 就像我说的那样,如果我因本地和产品服务器上的onkeyup事件而将其触发,则可以正常工作。

<script type="text/javascript">
    function setDefaultRadioButton(){
        alert("in setDefaultRadioButton()!!!");
        document.getElementById("Active").checked = true;
    }
</script>

Here's where I try to invoke it using onload(). 在这里,我尝试使用onload()调用它。 Like I said it fires when I run the app on my local server - just not when I deploy it to the production server... 就像我说的那样,当我在本地服务器上运行该应用程序时会触发-而不是在将其部署到生产服务器时触发...

<body onload="javascript:setDefaultRadioButton();">

I've tried just about everything I can think of - it just won't fire. 我已经尝试了几乎所有我能想到的-它不会触发。

Any suggestions? 有什么建议么?

Thanks! 谢谢!

Consider using window.onload . 考虑使用window.onload It is more appropriate for your case and inline javascript is not very good practice. 它更适合您的情况,并且内联javascript并不是很好的做法。

<script type="text/javascript">
    window.onload = function () {
        setDefaultRadioButton();
    }
</script>

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

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