Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现动态加载一个 JavaScript 资源 #4

Open
ziv-zjc opened this issue Jul 9, 2019 · 0 comments
Open

实现动态加载一个 JavaScript 资源 #4

ziv-zjc opened this issue Jul 9, 2019 · 0 comments

Comments

@ziv-zjc
Copy link
Owner

ziv-zjc commented Jul 9, 2019

实现动态加载一个 JavaScript 资源

 关键字:
        onreadystatechange
        loaded 
        complete 
        readyState
        onerror
var script = document.createElement(“script”);

var head = document.getElementsByTagName(“head”)[0];


script.type = “text/javascript”;

script.src = //i.alicdn.com/resource.js”;


// 绑定资源加载成功事件

script.onreadystatechange = function( ){

// 判断资源加载状态是否为加载成功或加载完成

if(/^(loaded|complete)$/. test (script.readyState)  ) {

script.onreadystatechange = null;

. . . .

}

};


// 绑定资源加载失败事件

script.onerror = function( ) {

. . . .

};


head.insertBefore (script , head.firstChild)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant