等待JavaScript中的动态文件加载

时间:2018-08-07 13:43:58

标签: javascript

我想通过代码加载一些CSS文件和JS文件。我知道这是异步的,所以我必须使用回调。

我必须支持IE 9。

我的ChromeOptions options = new ChromeOptions() DesiredCaps = options.ToCapabilities() as DesiredCapabilities; DesiredCaps.SetCapability("platformName","Android"); DesiredCaps.SetCapability("platformVersion", "8.1"); DesiredCaps.SetCapability("deviceName", "Device 01 Oreo_1440x2560"); DesiredCaps.SetCapability("device", "Android"); AppDriver = new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), DesiredCaps, TimeSpan.FromSeconds(300)); 执行此代码

initilizer.js

在这些回调中,代码由于某些js文件仍未加载而崩溃。他们仍然具有this.importCascadingStyleSheet = function(path, callback){ // load CSS file var element = document.createElement("link"); element.setAttribute("rel", "stylesheet"); element.setAttribute("type", "text/css"); element.setAttribute("href", path); this.appendComponent(element, function(){ callback(); }); } this.importScript = function(path, callback){ // load JS file var script = document.createElement('script'); script.src = path; script.type = "text/javascript"; this.appendComponent(script, function(){ callback(); }); } this.appendComponent = function(cmp, callback){ document.head.appendChild(cmp); callback(); } this.getLibPath = function(){ var dir = document.querySelector('script[src$="initilizer.js"]').getAttribute('src'); var fileName = dir.split('/').pop(); var parentDir = dir.replace('/' + fileName, ""); var directoryName = parentDir.split('/').pop(); return parentDir.replace('/' + directoryName, ""); } this.importStyle = function(fileName, callback){ // import css file with name this.importCascadingStyleSheet(this.libPath + "/style/" + fileName + ".css", function(){ callback(); }); } this.importSource = function(fileName, callback){ // import script with name this.importScript(this.libPath + "/client/" + fileName + ".js", function(){ callback(); }); } var me = this; me.libPath = me.getLibPath(); me.importStyle("myCssFile", function(){ me.importSource("firstCodeFile", function(){ me.importSource("secondCodeFile", function(){ me.importSource("thirdCodeFile", function(){ // ... Execute Script code here var foo = new Bar(); }) }) }) }); 状态。

我该如何解决?

0 个答案:

没有答案