简体   繁体   English

将JavaScript文件移动到称为HTML的Node.JS中的外部引用

[英]Moving javascript file to external reference in Node.JS called HTML

I have some javascript ajax code in my html page that I host on a Node.JS server. 我在Node.JS服务器上托管的html页面中有一些javascript ajax代码。

I would like to move it to a specific side .js file to be able to debug but can't figure out how. 我想将其移至特定的.js侧文件以进行调试,但不知道如何进行调试。 The solutions here doesn't work. 这里的解决方案不起作用。

It seems due to scope but not sure how to solve it. 似乎是由于范围,但不确定如何解决。

<script>
  $(document).ready(function() {
    $('#mainTable').DataTable( {
        "ajax": "jsonData",
        "columns": [
            { "data": "level" },
            { "data": "value" },
            { "data": "description" }
        ]
    } );
  } );
</script>

EDIT 编辑

Reason for doing so: 这样做的原因:

  1. Wishing to have a clean split bet. 希望有一个干净的对分投注。 script and html 脚本和HTML
  2. Being able to debug the scripting part (seems not possible in Visual Studio Code for the part that is in the html file) 能够调试脚本部分(对于html文件中的部分,在Visual Studio Code中似乎是不可能的)
  3. Solve some import issue that doesn't seem to work in the html script section 解决了一些在html脚本部分似乎不起作用的导入问题

For #3, see below as an example, the require doesn't work although path is correct: 对于#3,请参阅以下示例,尽管路径正确,但require无效:

<script>
    window.onload = function() {
    var a = document.getElementById("books");
    a.onclick = function() {
        var utils = require('./../controllers/utils.js');
        var filteredData = utils.filterJsonData(table, "HighLevel");
    }
</script>

Thank you in advance 先感谢您

In your html page add the following. 在您的html页面中添加以下内容。

<script src="./somename.js"></script>
<script src="./../controllers/utils.js"></script>

In your somename.js 在你的somename.js中

$(document).ready(function(){
  $('#mainTable').DataTable({
      "ajax": "jsonData",
      "columns": [
        { "data": "level" },
        { "data": "value" },
        { "data": "description" }
      ]
  });
  $('a#books').click(function(){
    //Where is table coming from?
    var filteredData = utils.filterJsonData(table, 'HighLevel');
  });
});

As for being easier to debug between a separate js vs in a single html page, it should not matter. 至于更容易在单独的js和单个html页面之间进行调试,则没关系。 It should be the same effort to debug in the browser. 在浏览器中进行调试应该花费相同的精力。

require works only in nodejs which can be used in server side js code. require仅在可在服务器端js代码中使用的nodejs中有效。 Are you using some library that provides require in browsers? 您是否正在使用某些在浏览器中提供require的库?

Move the code that is currently inside of your script tag to a separate file, say side.js : 将当前位于script标记中的代码移动到一个单独的文件,例如side.js

  $(document).ready(function() {
    $('#mainTable').DataTable( {
        "ajax": "jsonData",
        "columns": [
            { "data": "level" },
            { "data": "value" },
            { "data": "description" }
        ]
    } );
  } );

Change the script tag to be 将脚本标记更改为

<script source="./side.js"></script>

It looks from some of your other code ( require... ) that you are attempting to execute Node.js inside of the HTML. 从您尝试在HTML内部执行Node.js的其他一些代码( require... )来看。 Node.js is executed on the server side; Node.js在服务器端执行; all of the Javascript in the HTML will be executed on the client-side (browser). HTML中的所有Javascript都将在客户端(浏览器)上执行。


ETA: now that you've updated with the code, I see that, yes, you are attempting to use Node.js inside of the HTML. 预计到达时间:现在,您已经更新了代码,可以看到,是的,您正在尝试在HTML内使用Node.js。 It won't work - you can't require inside of the user's browser. 它不起作用-您require在用户的浏览器内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM