9

I have a console error that tells me this:

SyntaxError: '' string literal contains an unescaped line break

I have checked my syntax several times but I cannot correct the error. I'll copy the code to you if you can identify something.

    $('#nouveau').click(function(){
        $('#action').html('<?php echo preg_replace('/\s\s+/', '', $langs->trans("Delete") ); ?>');
        $('.titre_ecv').html('<?php echo addslashes(trim(preg_replace('/\s\s+/', '', $langs->trans("ecv_nouveau_formation")))); ?>');
        $('.nouveau').show();
        $('#valider').show();
        $id=$('#tr_formations tr').length+1; 
        $('#tr_formations').append('<tr><td><input name="action" type="hidden" value="create"><input type="text" name="formations['+$id+'][etablissement]" autocomplete="off" style="width:95%" /></td> <td style="width:10%;"> <select class="niveau flat" id="niveau" name="formations['+$id+'][niveau]" ><option value="0" ></option><option value="Qualifiant"  ><?php echo preg_replace('/\s\s+/', '', $langs->trans("Qualifiant") ); ?></option><option value="Bac" >Bac </option> <option value="Bac+1" >Bac+1 </option><option value="Bac+2" >Bac+2 </option><option value="Bac+3" >Bac+3 </option><option value="Bac+4" >Bac+4 </option><option value="Bac+5" >Bac+5 </option><option value="Master_specialisé" ><?php echo preg_replace('/\s\s+/', '', $langs->trans("Master_specialisé") ); ?> </option><option value="Master_recherche" ><?php echo preg_replace('/\s\s+/', '', $langs->trans("Master_recherche") ); ?> </option> <option value="Doctorat" >Doctorat </option></select> </td> <td ><input type="text"  name="formations['+$id+'][intitule]" autocomplete="off" style="width:95%" /></td> <td><input type="text"  name="formations['+$id+'][filiere]" autocomplete="off" style="width:95%" /></td><td style="width:8%;"><input type="text" name="formations['+$id+'][debut]" class="datepicker debut" value="<?php echo date('d/m/Y') ?>" onchange="MinDate(this);" autocomplete="off" /></td> <td style="width:8%;"><input type="text" name="formations['+$id+'][fin]" value="<?php echo date('d/m/Y') ?>" class="datepicker fin" onchange="MaxDate(this);" autocomplete="off" /><div align="center"><label><input type="checkbox" name="experiences['+$id+'][no_jours]" onchange="no_jourss(this);"> <b class="nos_jour"><?php echo trim(preg_replace('/\s\s+/', '', $langs->trans("ecv_no_jours") )); ?></b></label></div> </td><td align="center"><img src="<?php echo DOL_MAIN_URL_ROOT.'/theme/md/img/delete.png' ?>" class="img_delete" onclick="delete_tr(this);"></td></tr>');
        $( ".datepicker" ).datepicker({
            dateFormat: 'dd/mm/yy'
        });
        $(".niveau").select2()
    });

My job is to correct an existing code. Thank you for your help

David Maze
  • 130,717
  • 29
  • 175
  • 215
HeleneTitan
  • 99
  • 1
  • 1
  • 3
  • 3
    If you follow the line ` $('#tr_formations')` there seems to be an extra single quote somewhere that is putting the rest of the line out of sync. – half of a glazier Jan 07 '20 at 09:20
  • yes it is on this line that I am looking for. but I must have a problem in the eyes because I looked several times and I do not see ... I am trying to continue to look but without success. – HeleneTitan Jan 07 '20 at 09:36
  • Try breaking each single-quote string onto a new line, that will probably make it easier to find there the discrepancy is – half of a glazier Jan 08 '20 at 11:47
  • yes I did like that and indeed it allowed me to find the error. thank you – HeleneTitan Jan 09 '20 at 10:06

4 Answers4

7
 $('#action').html(...)

and

 $('.titre_ecv').html(...)

have mixed ' and ", so there should be the error. Although that <?php... should not work, because PHP runs on the server and not on the web browser.

Edit

That might work:

$('#action').html("<?php echo preg_replace('/\s\s+/', '', $langs->trans('Delete') ); ?>");
$('.titre_ecv').html("<?php echo addslashes(trim(preg_replace('/\s\s+/', '', $langs->trans('ecv_nouveau_formation')))); ?>");
Sixtus
  • 155
  • 8
  • Yes I had managed to correct errors on other parts of the code which indeed blocked php. I'm trying to change 'and "but my error is still the same. – HeleneTitan Jan 07 '20 at 09:51
  • it does not work . I tested the line and the part that does not work is the selection of levels. after $('#tr_formations') . it's hard to find where – HeleneTitan Jan 07 '20 at 11:15
  • The `trans('Delete') ); ?>")` with `.html(trans('Delete') )); ?>)` , additionally it won't get syntax highlighted. – gregn3 May 26 '20 at 11:13
2

Search engine brought me to this Q&A but the solution is totally different. It's posted just in case it helps others.

This error was searched:

Uncaught SyntaxError: '' string literal contains an unescaped line break

The line in question contained:

    alert('validUrlSyntax: ' + validUrlSyntax +
          ' validUrlExists: ' + validUrlExists +
          ' time to check: ' + elapsedTime + ' milliseconds`)

Notice the last apostrophe is really a back tick. Changing the back tick to an apostrophe ' solved the problem.

WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34
1

This may not be the answer for this particular question. But the same error occurs when you try to echo php in js as in this question. Sometimes it occurs because of the line breaks. So if you simply remove the line breaks in the string error can be corrected.

you can use,

$string = str_replace(array("\r", "\n"), '', $string);

to remove line breaks.

1

Try to include string with line breaks not into "" or '', but into `` on js side.