Hüseyin Okumuş Hüseyin Okumuş - 2 days ago 4
Node.js Question

Button Click in Jade without Script Tag

I saw this code for button click for Jade

button(onclick=foo)
script(foo(){alert('foo');})


I don't want any script tag or any JavaScript code in my rendered page .How can How can I make any button click in Jade from express or node.js file ?

Answer

You can use inline javascript :

button(onClick="javascript:alert('foo')")

However, this is not really optimal, and using a script tag/external script makes your code more readable and easier to debug.

You cannot however use a onclick event without using any JavaScript. If your constraints are 'no JS', you cannot have an alert on click (alert is also a javascript function)

If you wish to separate concerns as much as possible, you can have your JS code in a separate .js file, but will still need to include that file in your rendered page.

Edit : An explanation on server/client interactions :

When you create a server in Node/Express, you are generating assets (html pages) from your templates. This generation, as well as what do to when a user reaches a route within your site, are handled by the server.

The server "serves" the source code page (the doc you see by clicking 'view source' to the client, which then interprets it.

If the browser sees Javascript code, it knows how to read it and will add all the events, alerts, etc that you have placed in the code. it will then be able to process those interactions. Your button event in this case has nothing to do with Node/Express

TL;DR : the server only sends source code to the client, who's in charge of reading it and interpreting it.

Comments