Javascript 页面加载后运行javascript

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

Run javascript after page load

javascriptjquerypageload

提问by Benyamin

setup_account ui-mobile-viewport ui-overlay-c

setup_account ui-mobile-viewport ui-overlay-c

When a page i make loads like this:

当我加载一个页面时:

var location    =   location.href+"&rndm="+2*Math.random()+" #updatecomment0>*"
$("#updatecomment0").load(location, function(){});

I have multiple scripts running on the updatecomment0div:

我有多个脚本在运行updatecomment0div

<div id="updatecomment0">
    <div id="javascript1">hi</div>
    <div style="float:right;" class="javascript2">delete</div>
</div>

I don't know how to make this other JavaScripts run afterpage load.
Can someone please tell me how to with this.
Thank you

我不知道如何页面加载运行其他 JavaScript 。
有人可以告诉我如何处理这个。
谢谢

回答by Chetan

Use$(document).ready().

使用$(document).ready().

回答by Sreekumar P

Use jQuery, you can do this very easily.

使用jQuery,你可以很容易地做到这一点。

jQuery(document).ready(function(){
    alert('Your DOM is ready.Now below this u can run all ur javascript');
});

回答by Carls Jr.

Here is a sample layout for you

这是您的示例布局

<script type="text/javascript">
    $(document).ready(function(){
        /// here you can put all the code that you want to run after page load
        function Javascript1(){
            //code here
        }

        function Javascript2(){
            // code here
        }

        $("#btnOK").live('click',function(){
            // some codes here
            Javascript1();
            Javascript2();
        });

    });
</script>

<div id="MyDiv">
    <input type="button" id="btnOK" value="OK"/>
</div>

回答by diEcho

write code inside ready

在里面写代码 ready

jQuery(document).ready(function(){

// write here
});

suggestion : use liveor bind

建议:使用livebind

回答by ravndal

Unless you need javascript to do something before the page is loaded, add your scripts to the bottom om the html document, just before the bodyend tag. The page will load faster, and you can do whatever you need to, right in the js file, without the document ready functions.

除非您需要 javascript 在页面加载之前做一些事情,否则将您的脚本添加到 html 文档的底部,就在body结束标记之前。页面加载速度更快,您可以在 js 文件中执行任何您需要的操作,而无需准备文档功能。

If the scripts is the last to load, the DOM is already guaranteed to be "ready".

如果脚本是最后加载的,则 DOM 已经保证“就绪”。

回答by mferly

$(window).load(function() {
    // code here
});

回答by RxV

$(document).ready()is all you needed.

$(document).ready()就是你所需要的。

回答by Matteo

I hope this can help you too, I had a similar issue and I fixed it by calling the following just after loading the content in the page (like after an AJAX request for a page to be shown inside a div):

我希望这也能帮助你,我有一个类似的问题,我通过在页面中加载内容后调用以下来修复它(就像在 AJAX 请求将页面显示在 div 中一样):

(function($) {
 $(document).ready(function() {
  // your pretty function call, or generic code
 });
}(jQuery));

Remember to not call this in the document you load, but in the function that load it, after it as been loaded.

请记住不要在您加载的文档中调用它,而是在加载它之后,在加载它的函数中调用它。

回答by Josh Reeves

Using vanilla Javascript, this can done thusly:

使用 vanilla Javascript,可以这样做:

<html>
<head>
<script type="text/javascript">

// other javascript here

function onAfterLoad() { /*...*/ }

// other javascript here

</script>
</head>
<body>

<!-- HTML content here -->

<!-- onAfterLoad event handling -->
<div style="display:none;"><iframe onload="onAfterLoad();"></iframe></div>
</body>
</html>

回答by ravi

You can make JavaScript wait for a specified time using the setTimeoutmethod:

您可以使用以下setTimeout方法让 JavaScript 等待指定的时间:

.setTimeout("name_of_function()",time_in_millis);