How to make JavaScript execute after page load?

前端 未结 24 2398
孤城傲影
孤城傲影 2020-11-21 05:55

I\'m executing an external script, using a

24条回答
  •  梦谈多话
    2020-11-21 06:16

    Reasonably portable, non-framework way of having your script set a function to run at load time:

    if(window.attachEvent) {
        window.attachEvent('onload', yourFunctionName);
    } else {
        if(window.onload) {
            var curronload = window.onload;
            var newonload = function(evt) {
                curronload(evt);
                yourFunctionName(evt);
            };
            window.onload = newonload;
        } else {
            window.onload = yourFunctionName;
        }
    }
    

提交回复
热议问题