如何在页面加载后立即调用 javascript 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12457409/
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 08:00:20  来源:igfitidea点击:

how to call a javascript method soon after page load

javascriptjsp

提问by Java Questions

Possible Duplicate:
Javascript that executes after page load

可能的重复:
页面加载后执行的 Javascript

how to call a javascript method as soon as page is loaded.

如何在页面加载后立即调用 javascript 方法。

i have a java script which needs to be called soon after the jsp page is loaded, how to achieve this in javascript.

我有一个需要在jsp页面加载后立即调用的java脚本,如何在javascript中实现这一点。

could any one of you help me pelase.

你们中的任何人都可以帮助我 pelase。

Regards

问候

回答by Shiv Kumar Ganesh

You can write a code snippet something like this :-

您可以编写如下代码片段:-

window.onload(function(){
//Your JavaScript here
});

And if using JQuery then

如果使用 JQuery 那么

document.ready(function(){
//Your JavaScript Here
});

Or you can have all your JS after all the HTML.

或者你可以在所有的 HTML 之后拥有所有的 JS。

you can even use a function called :--

你甚至可以使用一个名为 :--

document.onload(function(){
//Your code Here
});

Last but not the least you could even try out this

最后但并非最不重要的,你甚至可以试试这个

 <body onload="YourJSMethod();">
        <!-- Some html content -->
    </body>

回答by Shades88

you can try

你可以试试

<body onload="someMethod();">
    <!-- Some html content -->
</body>

This will call your method as soon as body tag of your page is loaded. Alternatively you can make use of jquery's document ready function.

这将在加载页面的 body 标记后立即调用您的方法。或者,您可以使用 jquery 的文档就绪功能。

$(document).ready(function() { 
    // your code 
});