Jump to content

Putting put string text in javascript raise syntax error


Recommended Posts

Hello,


In my Laravel 8 / jQuery 3.5.1 app I need to put string text in javascript like :
 

L.marker([{{$adLocation->lat}}, {{ $adLocation->lng }}]).addTo(locationMap)
.bindPopup("<b>Add Location!</b><br />"+'{!! crlf($adLocation->content) !!}'+".").openPopup();


I try to replace crlf characters with method like

function crlf(string $s) : string
{
return str_replace( array("\r\n", "\r", "\n"), "<br />", $s);

// 2 lines below does not work too
// return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $s);
// return nl2br($s);
}


But anyway I got error :

edit:539 Uncaught SyntaxError: Invalid or unexpected token


and I see in browser : https://prnt.sc/wtpnri and https://prnt.sc/wtq78t

Which way is valid ?

Thanks!

Link to post
Share on other sites

Normal Javascript strings do not work across multiple lines.

If you're putting a value into Javascript then use @json instead. You should basically never ever use {!! for anything.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.