node.js - Pass Jade model variable to a view script function parameter

I want to pass a Jade model variable as a parameter to a JavaScript function.

I'm reading the socketUrl variable from config and passing it to the view:

app.get("/", function (req, res) {
    var socketUrl = config.get("socketUrl");
    res.render("index", {title: "Ticker", socketUrl: socketUrl});
});

Now I wont to use it in a view script ticker.initialize(socketUrl):

html
  head
    script(src="js/socket.io-1.4.5.js")
    script(src="js/ticker.js")
    script.
        document.addEventListener("DOMContentLoaded", function () {
            ticker.initialize(socketUrl);
        });

The browser cannot read socketUrl in that way. How to pass it correctly?

1 Answer

  1. Francis- Reply

    2019-11-16

    This should work but only with strings:

    script.
        document.addEventListener("DOMContentLoaded", function () {
            ticker.initialize("#{socketUrl}");
        });
    

    If it's an object try !{socketUrl}

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>