JavaScript 加载顺序 [英] JavaScript Load Order

查看:25
本文介绍了JavaScript 加载顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 amq.js (ActiveMQ) 和 Google 地图.我按这个顺序加载我的脚本

I am working with both amq.js (ActiveMQ) and Google Maps. I load my scripts in this order

<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <title>AMQ & Maps Demo</title>

    <!-- Stylesheet -->
    <link rel="stylesheet" type="text/css" href="style.css"></link>

    <!-- Google APIs -->
    <script type="text/javascript" src="http://www.google.com/jsapi?key=abcdefg"></script>

    <!-- Active MQ -->
    <script type="text/javascript" src="amq/amq.js"></script>
    <script type="text/javascript">amq.uri='amq';</script>

    <!-- Application -->
    <script type="text/javascript" src="application.js"></script>
</head>

但是在我的 application.js 中,它可以很好地加载地图,但是在尝试使用 AMQ 订阅主题时出现错误.AMQ 依赖于 Firefox 中的错误控制台说对象未定义的原型.我想我在脚本完成加载之前使用 amq 对象有问题.在我在 application.js 中使用这两个脚本之前,有没有办法确保它们都加载?

However in my application.js it loads Maps fine but I get an error when trying to subscribe to a Topic with AMQ. AMQ depends on prototype which the error console in Firefox says object is not defined. I think I have a problem with using the amq object before the script is finished loading. Is there a way to make sure both scripts load before I use them in my application.js?

Google 有一个很好的函数调用 google.setOnLoadCallback(initialize); ,效果很好.我不确定 amq.js 有没有这样的东西.

Google has this nice function call google.setOnLoadCallback(initialize); which works great. I'm not sure amq.js has something like this.

推荐答案

有没有办法确保在我在 application.js 中使用这两个脚本之前加载它们?

JavaScript 文件应该按顺序加载并阻塞,因此除非您依赖的脚本正在执行一些不寻常的操作,否则您需要做的就是在其他文件之后加载 application.js.

JavaScript files should load sequentially and block so unless the scripts you are depending on are doing something unusual all you should need to do is load application.js after the other files.

Non-blocking JavaScript Downloads 有一些关于脚本如何加载(并讨论了一些颠覆阻塞的技术).

Non-blocking JavaScript Downloads has some information about how scripts load (and discusses some techniques to subvert the blocking).

这篇关于JavaScript 加载顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆