Jade: load external javascript and call function

后端 未结 3 1882
再見小時候
再見小時候 2021-01-02 09:33

I was learning Express/Node/Jade and now in the Jade file I want to include a javascript file from the public folder just for the page. For example, in jade file I type this

3条回答
  •  悲哀的现实
    2021-01-02 10:11

    Well... In the first instance, it is different what happens in the browser of what happens on the server. So Jade is a rendering of HTML, therefore if you are in the browser. It's what ExpressJS shipping, ie rendering Jade. If you want to call, your HTML Javascript (Rendering of Jade), should show you where the Javascript. For exmaple

    in Server.js

    // Get the Javascript in the browser
    app.use("/javascripts", express.static("./outJavascripts"));
    // Get the URL
    app.all("/", function(req, res){
      // Render the Jade and Send for the client (Browser)
      req.render("myTemplate.jade");
    });
    

    In myTemplate.jade

    script(src='/javascripts/test.js')
    

    In "./outJavascripts/test.js"

    function check_test(){
        console.log("It's working! :D");
        return "It's working!";
    }
    

    If you do this, you will realize that it is run, the file "./outJavascripts/test.js" in the browser. And the function "check_test" never run in the server.

提交回复
热议问题