属性值未传递到其他脚本中的变量

时间:2019-05-02 13:10:43

标签: javascript jquery

我在服务器端有一个javascript。在运行某些功能后的脚本中,我得到的值特别是websocket的路径。我将该路径设置为javascript对象的属性。该javascript对象是一个聊天弹出窗口,其html存在于我的网站上。

myscript.js

//----------some_exisiting_code-----------
  var ws_path  = "ws://some-ip-address/chat/id/"  //This path is just an example.The path will be calculated dynamically by above existing lines of code.
  $(".chat-popup").attr("path",ws_path);
//----------some_exisiting_code-----------

mypage.html

//Importing myscript.js from server
  <script src="http://my-server-address/static/myscript.js"></script>

// passing value of "path" attribute as websocket endpoint
  <script> 
    var endpoint = $(".chat-popup").attr("path");
    var socket = new ReconnectingWebSocket(endpoint);
  </script>

  <div class="chat-popup">
   <div class="chat-history">   
    <ul id='chat-items'></ul>  
   </div>

   <div class="message">

 <!------------Message to be sent----------------------->
   <form id='form' method='POST'> 
     <input type="hidden" name="">
     <input id="id_message" type="text">        
     <input type='submit' class='btn btn-primary'/>
  </form>   
<!------------------------------------------------------------------>
</div></div>

我遇到错误:端点未定义

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以在页面的页脚中添加“ myscript.js”文件。您还可以添加“ jquery.min.js”文件。

myscript.js

Scanner in = new Scanner(System.in);
        System.out.println("Please enter the file name: ");
        String file = in.nextLine();
        try{
            Scanner inFile = new Scanner(new File(file));
            int count = 0;
            float average1 = 0;
            float average2 = 0;
            while (inFile.hasNextFloat()) {
                String str = inFile.nextLine();
                Scanner line = new Scanner(str);
                line.useDelimiter(" ");
                average1 += Float.parseFloat(line.next());
                average2 += Float.parseFloat(line.next());
                count++;
            }
            System.out.println("The average of the first column: " + average1 / count);
            System.out.println("The average of the second column: " + average2 / count);
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }

mypage.html

//----------some_exisiting_code-----------
var ws_path  = "ws://some-ip-address/chat/id/"  //This path is just an example.The path will be calculated dynamically by above existing lines of code.
//alert(ws_path);

$(".chat-popup").attr("path",ws_path);
//----------some_exisiting_code-----------