JavaScript: Escaping Special CharactersEvery programming language has it's special characters - characters that mean something special such as identifying a variable, the end of a line or a break in some data. JavaScript is no different, so it provides a number of functions that encode and decode special characters. If you're interacting between PHP and JavaScript you will also need to be familiar with the PHP functions for encoding and decoding special characters, which is why we've created this special tool for testing and comparing different functions. Encoding and decoding using JavaScript and PHPThe form below let's you see the output of various functions that are used to encode special characters when they appear in plain text or URL parameters (following the '?' in a URL). This page calls the PHP functions directly using Ajax rather than a JavaScript emulation. If you have a string to decode, use the buttons on the right instead. These functions perform replacements on certain characters as shown in the table futher down the page and described briefly here:
Escaping Double and Single QuotesAnother essential PHP function that comes in handy when passing data to JavaScript is addslashes which will add a backslash before: backslashes, single- and double-quotes. For example, to echo a PHP variable into JavaScript code: ... onclick="return confirm('Delete this item: <?PHP echo addslashes($name) ?>?';" ...
In the HTML we use double-quotes and in the JavaScript single-quotes, so any quotes within the JavaScript code will need to be escaped so that they don't conflict with either the HTML or JavaScript quotes. For more details on escaping PHP variables for use in JavaScript see our related article: Passing PHP variables to JavaScript. Table of encoded charactersHere you can see how the various JavaScript and PHP functions apply to a range of common characters.
The RFC 1738 specifications make fascinating reading - considering that the document is 10 years old yet still applicable. Related ArticlesReferencesFeedback and Questions2007-04-26: Praveen (Cognizant Technologies Solutions) says: When user copies the data from microsoft word to my PHP application this often inserts unidentified characters. Is there a permant solutions for this? Not if they insist on using Word. The problem is that Word uses it's own Character Set which uses characters that aren't compatible with a lot of websites or databases. 2008-01-12: Linda Fahlberg-Stojanovska says: THANK-YOU!!! I needed this table for my pmwiki and finally found it here and so beautifully organized. 2008-03-24: godlike says: very helpful page. Maybe you should add an encoding option so we can test the funcs with different encodings. That's a great idea, but I'm still a bit clueless when it comes to encoding - everything we do is locked down as UTF-8. If I could just work out where to start... 2008-09-21: Daniel Topp (TNI The Network Inc.) says: I noticed that the percent sign % doesn't properly decode in javascript. It will encode to %25, but gives a javascript error when trying to decode. Were you aware of this bug and are there any known fixes? The definition of the decodeURI function is that it "decodes a Uniform Resource Identifier (URI) previously created by encodeURI or by a similar routine" and "does not decode escape sequences that could not have been introduced by encodeURI". You will need to use a different function, or write your own, if your string is not already properly encoded. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
© Copyright 2009 Chirp Internet
- Page Last Modified: 26 November 2008
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||