如何在页面加载时在jsp中调用JavaScript函数而不使用< body onload =“" disableView()"" [英] How to call a javaScript Function in jsp on page load without using <body onload="disableView()">

查看:216
本文介绍了如何在页面加载时在jsp中调用JavaScript函数而不使用< body onload =“" disableView()""的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用页面加载的情况下调用JSP中的JavaScript函数

How can a JavaScript function in JSP be called during page load without using

<body onload="disableView()">

我必须在页面加载时在几个JSP中调用此函数,但JSP分为页眉,页脚和pageContent部分,并且body标记已放入页眉部分,并且只能在页眉部分中.因此,在某些页面中,我必须在pageContent部分的页面重新加载/加载中调用JavaScript函数,在该页面中我不会获得<body>标记来放入onload.我该如何解决呢?

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

推荐答案

使用 window.onload 这样

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

或者

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

(是,没有括号)

Or 只需将脚本放在页面的最底部,就在</body>之前.届时,所有HTML DOM元素都可以通过document函数进行访问.

Or just 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 document functions.

<body>
    ...

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

这篇关于如何在页面加载时在jsp中调用JavaScript函数而不使用&lt; body onload =“&quot; disableView()&quot;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆