Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

390
Visualizações
trying to include script in pug and call a function from the script, getting "unexpected token" error

I have a pug template where I want to call a function with some variables passed from the endpoint that renders the page:

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    button(type = 'button' onclick='play(#{x},#{y})')
    script(type='text/javascript' src='../public/javascripts/play.js')

Here is play.js:

const play = function(x,y){
    console.log(x+y);
}

When I load the page, I get this error:

Uncaught SyntaxError: Unexpected token ':'     play.js:1

How can I get pug to include the script correctly so that I can call the function with a button click?

EDIT: here's the rendered HTML:

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<button type="button" onclick="play(#{x},#{y})"></button><script type="text/javascript" src="../public/javascripts/play.js"></script>
</body>
</html>

EDIT 2:

I changed the interpolation to

onclick=`play(${x},${y})`

and this renders as:

onclick="play(2,3)"

which are the values that were passed in from the endpoint, but I'm still getting the unexpected token error.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda