user2837851
user2837851

Reputation: 1191

node.js jade template failed

I am practicing some jade code in node.js book. But it failed.with the error message.I had checked the syntax,and find no typo. Could someone give me an hint?Many thanks.

SyntaxError: /var/lib/stickshift/52d92cbee0b8cdfa010000ca/app-
root/data/743166/views/chat.jade:9
7| document.getElementById('chat').innerHTML =
8| '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
> 9| });
10| var submitChat = function(form) {
11| socket.emit('chat', {text: form.chat.value});
12| return false

(layout.jade)

doctype html
    html(lang='en')
        head
            title My Web Site
            block scripts

(chat.jade)

extends layout

block scripts
    script(type='text/javascript', src='/socket.io/socket.io.js')
    script(type='text/javascript')
        var socket = io.connect('http://localhost:8080');
        socket.on('chat', function(data) {
            document.getElementById('chat').innerHTML =
            '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
        });
        var submitChat = function(form) {
        socket.emit('chat', {text: form.chat.value});
        return false;
    };

block content
    div#chat
        form(onsubmit='return submitChat(this);')
            input#chat(name='chat', type='text')
            input(type='submit', value='Send Chat')

Upvotes: 0

Views: 1342

Answers (1)

MildlySerious
MildlySerious

Reputation: 9170

Add a . to the end of a tag to declare its contents as text instead of jade markup. This way you can write JS inside the script tag.

script(type='text/javascript').
    var socket = io.connect('http://localhost:8080');
    socket.on('chat', function(data) {
        document.getElementById('chat').innerHTML =
        '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
    });

Upvotes: 1

Related Questions