Home  >  Article  >  Web Front-end  >  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
WBOYOriginal
2016-05-16 16:38:031615browse

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'!');

Usually it will be changed to the following statement

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

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!');

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 (34b464afc13ce069d1a6de87eefe85df) /u003E
Asterisk (*) /u002A
& &
' '(/u0027)
" "(/u0022)
cccf46dbc9f695fe0e87ffdc659e8b8a >

Statement:
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