JavaScript的加载顺序 [英] JavaScript Load Order

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

问题描述

嘿,我既 amq.js (ActiveMQ的)和的谷歌地图。我打开我的脚本按照此顺序

 < HEAD>
    < META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
    <标题>&AMQ放大器;地图演示< /标题>    &所述;! - 样式 - >
    <链接rel =stylesheet属性类型=文/ CSS的href =style.css文件>< /链接>    <! - 谷歌的API - >
    <脚本类型=文/ JavaScript的SRC =htt​​p://www.google.com/jsapi?key=abcdefg>< / SCRIPT>    &所述;! - 主动MQ - >
    <脚本类型=文/ JavaScript的SRC =AMQ / amq.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的> amq.uri ='AMQ';< / SCRIPT>    &所述;! - 应用 - >
    <脚本类型=文/ JavaScript的SRC =的application.js>< / SCRIPT>
< /头>

然而,在我的application.js它加载地图不错,但尝试预订与AMQ主题时,我得到一个错误。 AMQ取决于原型,在Firefox错误控制台说,对象是没有定义。我觉得我有使用AMQ对象脚本加载完成前一个问题。 有没有一种方法,以确保之前,我在application.js中使用这两个脚本加载?

谷歌有这个漂亮的函数调用google.setOnLoadCallback(初始化);伟大的工程。我不知道amq.js有这样的事情。


解决方案

  

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


JavaScript文件应按顺序加载的和块的,除非你是依赖于正在做的事情不同寻常的一切,你应该需要做的脚本是其他文件后,负载的application.js。

非阻塞的JavaScript下载有一些信息是如何脚本加载(和讨论一些技巧来颠覆块)。

Hey 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>

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 has this nice function call google.setOnLoadCallback(initialize); which works great. I'm not sure amq.js has something like this.

解决方案

Is there a way to make sure both scripts load before I use them in my 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 has some information about how scripts load (and discusses some techniques to subvert the blocking).

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

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