我正在动态创建一些脚本,如下所示:
var js1 = document.createElement('script');
js1.type = 'text/javascript';
js1.src = '/js-1.js';
docmuent.body.appendChild(js1);
js1.onload = function() {
var js2 = document.createElement('script');
js2.type = 'text/javascript';
js2.src = '/js-2.js';
document.body.appendChild(js2);
};这是我的规范:
it('Tests inject script', function() {
expect($("script[src*='/js-1.js']").length).toBeGreaterThan(0);
expect($("script[src*='/js-2.js']").length).toBeGreaterThan(0);
});在js-2上的测试总是失败。如何侦测js-1 onload事件?
发布于 2013-12-30 19:35:07
我想问题可能出在你的订单上。您应该创建script标记,将其添加到body,设置onload,然后设置src。
var js1 = document.createElement('script');
js1.type = 'text/javascript';
docmuent.body.appendChild(js1);
js1.onload = function() {
var js2 = document.createElement('script');
js2.type = 'text/javascript';
document.body.appendChild(js2);
js2.src = '/js-2.js';
};
js1.src = '/js-1.js';发布于 2014-07-01 19:37:13
您不应该在onload事件中添加标记创建代码。如果我没记错的话,IE只会在脚本标签添加到DOM后立即开始下载文件。
我的建议是将onload事件中的所有内容移到外部,也许在onload事件内部有一个处理函数,以确定文件何时完成加载。这样,您唯一的问题将是测试onload事件内部的函数调用。您可以通过模拟脚本(‘document.createElement’);并返回一个伪对象,您可以像"fakeObject.onload()“一样使用该对象,并在内部辅助函数被调用时进行侦察。
https://stackoverflow.com/questions/20837973
复制相似问题