简体   繁体   English

静态加载脚本并在加载时运行

[英]Loading script statically and run when it is loaded

I am placing a script as the following: 我将脚本放置如下:

<script src="https://cdn.jsdelivr.net/blabla.js" type="text/javascript" id="the_script"></script>

In jquery I can: 在jQuery中,我可以:

$("#the_script").ready(function(){
    alert("loaded")
});

But I am not using JQuery. 但是我没有使用JQuery。 What is the equivalent in vanilla js (native). 什么是vanilla js(本机)中的等效项。

here is an example in jsfiddle http://jsfiddle.net/bmhm65vm/ 这是jsfiddle http://jsfiddle.net/bmhm65vm/中的示例

Running a script as soon as a browser loads (meaning before anything else) 浏览器加载后立即运行脚本(意味着先执行其他操作)
requires you to put the code you want to execute first into the following function: 要求您首先将要执行的代码放入以下函数中:

window.onload = function(){
    //Your Code Here.
};

The block of code inside of the window.onload function will be run FIRST. window.onload函数内部的代码块将首先运行。

You can always use onload like in this fiddle http://jsfiddle.net/est4dsav/ 您总是可以像这样在小提琴中使用onload http://jsfiddle.net/est4dsav/

Here you have all the native options: http://www.w3schools.com/jsref/event_onload.asp 在这里,您具有所有本机选项: http : //www.w3schools.com/jsref/event_onload.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM