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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5428418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:09:44  来源:igfitidea点击:

How to call a javaScript Function in jsp on page load without using <body onload="disableView()">

javascriptjsp

提问by Warrior

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. 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 onloadin. How can I solve this?

我必须在页面加载时在一些 JSP 中调用此函数,但 JSP 分为页眉、页脚和 pageContent 部分,而正文标记放入页眉部分,它必须仅在页眉部分。因此,在某些页面中,我必须在 pageContent 部分中的页面重新加载/加载时调用 JavaScript 函数,在该部分中我将无法<body>插入标签onload。我该如何解决这个问题?

回答by BalusC

Eitheruse window.onloadthis way

要么window.onload这种方式

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

or alternatively

或者

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

(yes, without the parentheses)

(是的,没有括号)



Orjust put the script at the very bottom of page, right before </body>. At that point, all HTML DOM elements are ready to be accessed by documentfunctions.

或者只是将脚本放在页面的最底部,就在</body>. 此时,所有 HTML DOM 元素都已准备好可供document函数访问。

<body>
    ...

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