简体   繁体   English

如何在页面加载时调用jsp中的javaScript函数而不使用<body onload =“disableView()”>

[英]How to call a javaScript Function in jsp on page load without using <body onload=“disableView()”>

How can a JavaScript function in JSP be called during page load without using 如何在页面加载期间调用JSP中的JavaScript函数而不使用

<body onload="disableView()">

I have to call this function in a few JSP's on page load but JSP's are divided into header, footer and pageContent sections and the body tag droped into header section and it has to be in header section only. 我必须在页面加载的几个JSP中调用此函数,但是JSP分为页眉,页脚和页面内容部分,并且正文标记已经变为标题部分,并且它必须仅在标题部分中。 So in some of the pages I have to call JavaScript function on page reload/load in pageContent section where I won't get <body> tag to put the onload in. How can I solve this? 所以在某些页面中我必须在pageContent部分的页面重新加载/加载时调用JavaScript函数,在那里我不会得到<body>标记来放入onload 。我该如何解决这个问题?

Either use window.onload this way 要么使用window.onload这样

<script>
    window.onload = function() {
        // ...
    }
</script>

or alternatively 或者

<script>
    window.onload = functionName;
</script>

(yes, without the parentheses) (是的,没有括号)


Or just put the script at the very bottom of page, right before </body> . 或者只是将脚本放在页面的最底部,就在</body>之前。 At that point, all HTML DOM elements are ready to be accessed by document functions. 此时,所有HTML DOM元素都可以通过document功能进行访问。

<body>
    ...

    <script>
        functionName();
    </script>
</body>

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

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