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

218
Visualizações
Illegal token in synchronous Javascript sent back from PHP response, regards cross site widget development

I have a widget on the test subject website; below.

<div id="widget"></div>
<script src="http://www.project.com/api/?api=bXOIo4ERTaZt-a5b71a1c" type="text/javascript"></script>
<script>
     
    initialise(); 
    
</script>

The widget communicates with the project website without any hitches. Except, when I have the PHP in the project website; like so.

function initialise() { 
    var container = 'widget'; 
    var ele = document.getElementById( container );
    
    var response = "<?php foreach( $this -> get( 'api:bestsellers' ) as $record ): ?><p><?php echo $record -> get( 'title' ); ?>, <?php echo $record -> get( 'format_price' ); ?></p><br><?php endforeach; ?>";

    ele.innerHTML = response;
}

This is the PHP on the project website, called by the widget. This works grand except when I put the PHP on a new line (for example indention and so on) I get an error on the developer tools; like so.

var response = "
<?php foreach( $this -> get( 'api:bestsellers' ) as $record ): ?><p><?php echo $record -> get( 'title' ); ?>, <?php echo $record -> get( 'format_price' ); ?></p><br><?php endforeach; ?>";

I get this: "Uncaught SyntaxError: Invalid or unexpected token"

Points to note are: 1) I am using synchronous Javascript for a reason and asynchronous Javascript/AJAX isn't an option, because I need multiple widgets on the same page, 2) my field of expertise is PHP and not Javascript, 3) I have tried (Javascript) string replacement on non UTF8 characters, line breaks and tabs, etc and invisible characters on the response.

Kind of lost as to why I can't use indention on the PHP block, any suggestions to get the response back to the widget in one piece, with no errors or faults? Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The error happens because the variable response's string is on multiple lines. To make a string multiple lines, you use backticks. Try this:

var response = `
<?php foreach( $this -> get( 'api:bestsellers' ) as $record ): ?><p><?php echo $record -> get( 'title' ); ?>, <?php echo $record -> get( 'format_price' ); ?></p><br><?php endforeach; ?>`;
// Did not give an error!

about 4 years ago · Juan Pablo Isaza Relatório
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