257

I'm trying to get JavaScript to render on my page using Jade (http://jade-lang.com/)

My project is in NodeJS with Express, eveything is working correctly until I want to write some inline JavaScript in the head. Even taking the examples from the Jade docs I can't get it to work what am I missing?

Jade template

!!! 5
html(lang="en")
  head
    title "Test"
    script(type='text/javascript')
      if (10 == 10) {
        alert("working")
      }
  body

Resulting rendered HTML in browser

<!DOCTYPE html>
<html lang="en">
<head>
  <title>"Test"</title>
  <script type="text/javascript">
    <if>(10 == 10) {<alert working></alert></if>}
  </script>
</head>
<body>
</body>
</html>

Somethings definitely a miss here any ideas?

jedierikb
  • 12,752
  • 22
  • 95
  • 166
JMWhittaker
  • 3,633
  • 3
  • 23
  • 30

8 Answers8

411

simply use a 'script' tag with a dot after.

script.
  var users = !{JSON.stringify(users).replace(/<\//g, "<\\/")}

https://github.com/pugjs/pug/blob/master/packages/pug/examples/dynamicscript.pug

Kallzvx
  • 594
  • 7
  • 23
liangzan
  • 6,804
  • 4
  • 29
  • 28
  • Good solution, but it will produce only ` – Vojto Jun 13 '12 at 09:12
  • 51
    `type="text/javascript"` is the default value for the `type` field on ` – Adam Fabicki Dec 18 '12 at 21:03
  • What about multiline code? Is there a way to have proper code indentation in my Javascript when embedded in Jade this way? – missingfaktor Feb 14 '13 at 07:50
  • 6
    Jade's policy changed, the inline script tag should now have a `.` appended. So `script.` followed by your indented block of JS. – joeytwiddle Aug 27 '13 at 20:26
  • I don't know about indentation missingfaktor. Perhaps save your users some bandwidth by sending js-beautify, so you can prettify the code on the client-side. ;) – joeytwiddle Aug 27 '13 at 20:28
  • 4
    That example is a script injection vulnerability. See https://github.com/visionmedia/jade/issues/1474 – Jason Merrill Mar 25 '14 at 17:53
  • Notably, all your server-side variables are still injectable in the script using `#{var_name}` – happyhardik Apr 22 '20 at 07:08
118

The :javascript filter was removed in version 7.0

The docs says you should use a script tag now, followed by a . char and no preceding space.

Example:

script.
  if (usingJade)
    console.log('you are awesome')
  else
    console.log('use jade')

will be compiled to

<script>
  if (usingJade)
    console.log('you are awesome')
  else
    console.log('use jade')
</script>
Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
62

Use script tag with the type specified, simply include it before the dot:

script(type="text/javascript").
  if (10 == 10) {
    alert("working");
  }

This will compile to:

<script type="text/javascript">
  if (10 == 10) {
    alert("working");
  }
</script>
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Stefan Jarina
  • 723
  • 5
  • 6
33

No use script tag only.

Solution with |:

script
  | if (10 == 10) {
  |   alert("working")
  | }

Or with a .:

script.
  if (10 == 10) {
    alert("working")
  }
Olivier C
  • 899
  • 1
  • 13
  • 14
3

THIRD VERSION OF MY ANSWER:

Here's a multiple line example of inline Jade Javascript. I don't think you can write it without using a -. This is a flash message example that I use in a partial. Hope this helps!

-if(typeof(info) !== 'undefined')
  -if (info)
    - if(info.length){
      ul
        -info.forEach(function(info){
          li= info
      -})
  -}

Is the code you're trying to get to compile the code in your question?

If so, you don't need two things: first, you don't need to declare that it's Javascript/a script, you can just started coding after typing -; second, after you type -if you don't need to type the { or } either. That's what makes Jade pretty sweet.

--------------ORIGINAL ANSWER BELOW ---------------

Try prepending if with -:

-if(10 == 10)
  //do whatever you want here as long as it's indented two spaces from
   the `-` above

There are also tons of Jade examples at:

https://github.com/visionmedia/jade/blob/master/examples/

JohnAllen
  • 7,317
  • 9
  • 41
  • 65
  • Thanks John have used that for the odd single line, however I can't see anyway of doing multiple lines without prepending it with the '-'. The Jade homepage ([link](http://jade-lang.com/) even has an example of what i'm trying to do, but it won't compile. I'm using the latest release too. – JMWhittaker May 02 '11 at 17:24
  • So you're asking how to have multiple lines of Javascript code below one if? – JohnAllen May 02 '11 at 17:26
  • @Bluey same here, I've never gotten this to work. You should ask on the github issues. – Mark May 05 '11 at 04:22
  • Mark, have now moved to using Sam Stephenson's Eco instead of Jade. I was only using jade as a quick UI for testing. – JMWhittaker May 06 '11 at 08:57
  • Jade 0.12.4 allows me to script() without prepending the JS with - below it. – Richard Holland Jul 27 '11 at 03:20
  • OP asked how to put inline javascript that would execute on the client side - This creates inline javascript that executes on the *server side* before returning the view to the user. – Dan May 11 '15 at 09:01
1
script(nonce="some-nonce").
  console.log("test");

//- Workaround
<script nonce="some-nonce">console.log("test");</script>
Ransomware0
  • 427
  • 3
  • 10
1

For multi-line content jade normally uses a "|", however:

Tags that accept only text such as script, style, and textarea do not need the leading | character

This said, i cannot reproduce the problem you are having. When i paste that code in a jade template, it produces the right output and prompts me with an alert on page-load.

JPanneel
  • 126
  • 2
0

Use the :javascript filter. This will generate a script tag and escape the script contents as CDATA:

!!! 5
html(lang="en")
  head
    title "Test"
    :javascript
      if (10 == 10) {
        alert("working")
      }
  body
Chris B
  • 9,149
  • 4
  • 32
  • 38
  • @aaaidan You're right. It's supported by Scalate (I'm using it here with no problems: https://github.com/cb372/phone-home/blob/master/src/main/webapp/WEB-INF/templates/views/recent-events.jade#L7 ) but it looks like pure Jade doesn't support it, as it's not listed here: https://github.com/visionmedia/jade#features . That's a shame! – Chris B Jul 11 '13 at 00:59
  • Yeah, would have been nice. As the top answer says, a `script.` works fine, and is legal `!!! 5` – aaaidan Jul 11 '13 at 10:23