Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quote escape in JavaScript Alert() function

I am showing some French line through javascript alert function and the French line looks like:

S'il vous plaît accepter les termes et conditions avant de procéder

And alert look like in page source:

alert('S'il vous plaît entrer une adresse email valide!');

Firebug is showing an error message like:

SyntaxError: missing ) after argument list

I try to escape the French line like:

alert('S\\'il vous plaît entrer une adresse email valide!');
alert('S\\\'il vous plaît entrer une adresse email valide!');
alert('S"'"il vous plaît entrer une adresse email valide!');

All guidelines were followed, but nothing works. So how do I fix it?

like image 915
Thomas Avatar asked Oct 09 '13 12:10

Thomas


2 Answers

Try -

alert("S'il vous plaît entrer une adresse email valide!");

This is probably the simplest approach. Whenever you need to pass a single quote in a string, wrap it inside a double quote, and vice versa.

If you have a mix of single and double quote in a string, then wrap it inside either single quotes or double quotes, and escape the corresponding ones in the string using a single backslash -

alert("So she said, \"Hey!, how are you?\". I said, 'I am fine, thanks'.") 
like image 99
MD Sayem Ahmed Avatar answered Sep 20 '22 12:09

MD Sayem Ahmed


A single backslash.

alert('S\'il vous plaît entrer une adresse email valide!')

like image 40
Slavo Avatar answered Sep 19 '22 12:09

Slavo