【问题标题】:include the JS source in the existing *.js file在现有的 *.js 文件中包含 JS 源代码
【发布时间】:2016-02-25 04:14:05
【问题描述】:

我有 MqttConnect.js 文件和 mqttws31.js 库。我必须将 mqttws31.js 的所有源代码都包含在我的 MqttConnect.js 文件中,这怎么可能?。

当我从 mqttws31.js 和过去的 mqttconnect.js 文件中复制所有内容时,就会出现此错误:

ReferenceError: 消息未定义

如果我尝试这种方式,它工作正常:

<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
     <script src="http://www.hivemq.com/demos/websocket-client/js/mqttws31.js" type="text/javascript"></script> 

     <script src="MqttJS/MqttConnect.js"></script>

</head>

MqttConnect.js 文件代码:

// Using the HiveMQ public Broker, with a random client Id

        var client = new Messaging.Client("broker.mqttdashboard.com",8000, "myclientid_" + parseInt(Math.random() * 100, 10));


        //Connect Options
        var options = {
            timeout: 60,
            keepAliveInterval:450, 
            cleanSession:false, 
            //Gets Called if the connection has sucessfully been established
            onSuccess: function () {

                alert("Connected:");

            },
            //Gets Called if the connection could not be established
            onFailure: function (message) {
                alert("Connection failed -: " + message.errorMessage);
            }
        };

        function Connect(){
            try {

            client.connect(options)



            }
            catch(err){
                alert(err.message);

            }

        }

mqttws31.js 代码:

http://www.hivemq.com/demos/websocket-client/js/mqttws31.js

更新

我想用这个的地方,没有html页面

【问题讨论】:

  • 您是否已将 mqttws31.js 文件中的所有内容复制到顶部的 MqttConnect.js 中?我的意思是mqttws.js文件的内容,然后是MqttConnect.js文件。
  • 我把最底下的东西都复制了,好好做吧
  • 是的,MqttConnect.js 的内容定义了在MqttConnect.js 文件中使用的Messaging 函数。因此应该首先加载MqttConnect.js 文件的内容。
  • 还是报错 ReferenceError: "window" is not defined.

标签: javascript jquery mqtt


【解决方案1】:

这可能是由于 JavaScript 加载方式的怪异。你可以在 this answer 中找到一个很好的例子来说明它应该如何完成。

快速的答案是将两个 JavaScript 文件的加载放入托管它们的 HTML 文档的正文中,MQTT 库位于您的脚本之上。

不要只是将库复制到您自己的文件中,如果您没有正确注明库的来源,那将是非常糟糕的形式并且侵犯了版权。

【讨论】:

  • 但我别无选择。仍然是一个错误 ReferenceError: "window" is not defined。
  • 'window' 在库中用于获取网页的 DOM,本质上意味着执行脚本时页面尚未完成加载。您可以尝试在脚本标签中添加“延迟”,这可能有助于解决这个问题,因为它会将 JS 加载延迟到页面加载之后 - 通常。
  • 我想用这个的地方,没有html页面
  • 那么这个库就不行了。它旨在使用由 Web 浏览器实现的 Web 套接字,因此如果您使用此服务器端(如下所示),那么您将需要为您的特定环境找到另一个解决方案。
【解决方案2】:

mqttws31.js的内容复制到顶部(不是底部)的MqttConnect.js,然后加载MqttConnect.js文件:

<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
    <script src="MqttJS/MqttConnect.js"></script>

</head>

我自己试过了,我没有收到任何错误。 (窗口未定义)

【讨论】:

  • 我想用这个的地方,没有html页面
  • 好的,你想在哪里使用它?
  • 我想使用它,http 适配器中的 IBM Mobilefirst 服务器端作为这个 qusentions ansewer stackoverflow.com/questions/35579623/…
  • 哦,明白了。我相信你不能包含这个 js 文件。我不确定 MobileFirst 的环境与浏览器有多相似
  • 不可能!你能建议我如何包含这个文件吗?一点点编辑?
【解决方案3】:

这两个文件之间存在依赖关系,即MqttConnect.js中有代码,需要mqttws31.js中的代码才能正常工作。所以我假设你在 MqttConnect.js 的末尾粘贴了 mqttws31.js 的内容。在 MqttConnect.js 的开头粘贴 mqttws31.js 的内容应该可以解决此问题。你的 MqttConnect.js 应该看起来像

// Contents of mqttws31.js ...
// Contents of MqttConnect.js ...

【讨论】:

  • 还是报错 ReferenceError: "window" is not defined.
猜你喜欢
  • 2017-12-22
  • 2012-07-27
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多