Frage

I want to use a variable which I defined in a JS script in my jade file. Is that possible ?

To be clear enough, here's what I'm trying to do :

  script.
   var J1 = 0;
   var socket = io.connect('http://localhost:3000');
   socket.on('positionJ1', function(positionJ1) {
     J1 = positionJ1;
   })

p Le joueur 1 est en case #{J1}

EDIT : To try the method with the DOM, I've done that, but I don't receive any alert. It isn't the innerHTML method that doesn't work, because when I do document.body.innerHTML = "Something"; It works well.

script.
    var J = document.getElementById("posJ1").innerHTML;
    alert(J);

p#posJ1 Plop

Correction : It works when my p#posJ1 is above the script

Solution :

p#posJ1
   script.
    var socket = io.connect('http://localhost:3000');
    socket.on('positionJ1', function(positionJ1) {
      var J1 = document.getElementById("posJ1");
      J1.innerHTML = "Le joueur 1 est en case " + positionJ1;
      document.body.appendChild(J1);
    })
War es hilfreich?

Lösung

This is literally impossible. Jade is rendered into static HTML before being served to the client. Whatever variables you put in are parsed at the time of rendering only.

You're gonna have to go with javascript to do any run-time DOM manipulations like that.

  script.
   var J1 = 0;
   var socket = io.connect('http://localhost:3000');
   socket.on('positionJ1', function(positionJ1) {
     J1 = positionJ1;
     document.getElementById('position').innerHTML= J1;
   })

    p Le joueur 1 est en case 
        span#position

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top