如何调用外部js文件中的函数?
注意:这里外部js文件将异步加载。
HTML文件:
<script type="text/javascript">
(function (e, b) {
var a, f, i, g;
a = e.createElement("script");
a.type = "text/javascript";
a.async = true;
a.src = "http://example.com/a.js";
f = e.getElementsByTagName("script")[0];
f.parentNode.insertBefore(a, f)
})(document, window.d || []);
</script>
外部js,即a.js:
var d = function () {
d.prototype.init = function () {
alert("test1");
} ; };
我无法从html文件中调用init方法。当我调用d.init();我收到一个像init不是函数的错误。请让我知道如何调用这些方法。
答案 0 :(得分:0)
您拨打电话时可能无法加载您的文件。如果您使用类似jQuery的getScript来加载外部文件,那么您将拥有一个成功函数,从那里调用您的init函数是安全的。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/spring-config.xml")
public class DaoTest {
@Autowired
private IOrdenMgmtDao ordenDao;
@Test
public void testDAO(){
Assert.assertNotNull(ordenDao);
Integer countForOrden = ordenDao.getCountByState("NEW");
Assert.assertEquals(new Integer(3), countForOrden);
List<OrdenDetailStateResult> ordenDetail = ordenDao.getOrderDetail("NEW");
Assert.assertNotNull(ordenDetail);
for (OrdenDetailStateResult ordenDetailStateResult : ordenDetail) {
System.out.println(ordenDetailStateResult.getOrden_id() + " "
+ ordenDetailStateResult.getComprador_email() + " "
+ ordenDetailStateResult.getStatus());
}
}
}
答案 1 :(得分:0)
您可以在代码中添加以下处理程序:
a.onload = function () {
new d().init();
}