How to show the result of a javascript operation to jade

1

The goal is to obtain the data of an operation javascript and show the result on the page, the code is as follows:

doctype html
html
 head
  title Mi Pagina

  script(type="text/javascript").
   var resultado = localStorage.getItem("valor");

 body
  h1 resultado

I am using the localStorage to be able to get values that I insert from another page and that I need to show in this one, but I already try different options to show it and they do not work for me.

h1 #{resultado}
h1=resultado

If I use the character option - with jade it does not work since it throws me an error that mentions that the function getItem does not exist

var resultado = localStorage.getItem("valor")
h1=resultado
    
asked by Tomochan 06.07.2016 в 19:00
source

1 answer

1
  • the element must have an ID (using the # in front and pasted h1#resultado )
  • to put text into a common element (which is not an input or a textArea or similar) textContent is used (in the old IE innerText), never innerHTML because it is insecure because of code injection and character escaping.
  • The script section should be placed in the body to be rendered faster
  • ejemplo:

    doctype html
    html
      head
        title Mi Pagina
    
      body
        h1#resultado
        script(type="text/javascript").
          resultado.textContent = localStorage.getItem("valor");
    

    It might not work because the elements are not ready yet, better put the script in an event:

    doctype html
    html
      head
        title Mi Pagina
    
      body
        h1#resultado
        script(type="text/javascript").
          window.addEventListener('load', function(){
            resultado.textContent = localStorage.getItem("valor");
          });
    
        
    answered by 06.07.2016 / 21:57
    source