Home > Web Front-end > JS Tutorial > Solutions to single and double quotation mark errors in JavaScript_javascript tips

Solutions to single and double quotation mark errors in JavaScript_javascript tips

WBOY
Release: 2016-05-16 16:38:03
Original
1685 people have browsed it

When using JavaScript to display messages or transfer character data, you often encounter single quotes (') or double quotes (") included in the data. Such statements often cause JavaScript errors. Generally use /' or /" solution.

For example:

Alert("this is test "message"!"); 
Alert('this is test 'message'!');
Copy after login

Usually it will be changed to the following statement

Alert("this is test /"message/"!"); 
//或者 
Alert("this is test 'message'!"); 
Alert('this is test /'message/'!');
Copy after login

If the above is only used in scripts or there is no too complicated data character connection, this problem has been solved.

But if there are more complex data character connections, especially some JavaScript statements composed by the server, you can consider using single quotes (') and double quotes ("), and using escape sequences (escape sequence) Output.

For example, the above statement can be converted into the following format:

Alert("this is test /u0022message/u0022!"); 
Alert('this is test /u0027message/u0027!');
Copy after login

Add some commonly used codes

Character description Unicode escape sequence
em dash (—) /u2014
Registration symbol (R) /u00AE
Copyright symbol (c) /u00A9
Trademark symbol (?) /u2122
Euro symbol (?) /u20AC
Backslash (/) /u005C
Forward slash (/) /u002F
Opening brace ({) /u007B
Right brace (}) /u007D
Less than sign (<) /u003C
Greater than sign (>) /u003E
Asterisk (*) /u002A
& &
' '(/u0027)
" "(/u0022)
< <
> >

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template