Escape an apostrophe under javascript

Last update on February 27, 2009 08:28 AM by aakai1056
Published by aakai1056

Escape an apostrophe under javascript







In Javascript, an apostrophe (single quote) into a string itself enclosed by apostrophes cause an error.

To troubleshoot, you should escape the apostrophe by heading the backslash character to get to the Javascript interpreter that it is an apostrophe within the string.

However, when the string is inserted dynamically, you should escape any characters that can cause an error (apostrophe, quote or double quote, backslash).

addslashes () under PHP is a useful feature for automating the escape special characters. The function below is an excellent alternative for JavaScript:

function addslashes (ch) (
ch.replace ch = (/ \ \ / g, "\ \ \ \")
ch.replace ch = (/ \ '/ g, "\ '")
ch.replace ch = (/ \ "/ g," \ \ \ "")
return c.
)
Best answers for « Escape an apostrophe under javascript » in :
Javascript - split () Show [Javascript] The split () function The split () function allows you to split a string and return the results in a table, using a string defined as a separator. In the example below, the split () can cut the date according to the...
Javascript - trim() function Show [Javascript] The trim() function The trim () function is native PHP (and other languages) and is used to remove the spaces at the beginning and end of the string. There is no such function in Javascript, but you can use the...
[Javascript]Knowing the height of an HTML element Show[Javascript]Knowing the height of an HTML element To determine the height of a HTML block using JavaScript, independently from the type browsers used, there are two methods: element.offsetHeight...
[JavaScript] Unit Converter Show[JavaScript] Unit Converter The script below defines a converter for units of measurements that works with Javascript only and that can be used locally in the browser, without the need for server code. //...
Javascript - Link Random / Choosing a random link ShowJavascript - Link Random / Choose a random link The script below creates a hyperlink directing the visitor to an address chosen at random from a list. The code below is to be inserted between the tag and to...
Download HTML Ebook Maker and Encrypter ShowAs its name suggests, this software will allow you to encrypt your HTML pages with a password. It supports the following files: HTML, image, flash, swf, video, Javascript and so forth. The encrypted files will be collected in a single .exe file....
Special characters ShowSpecial character codings HTML standards require all code to be written in 7-bit ASCII, which means that accented characters are not allowed. Despite this, current browsers recognise accented characters; you can enter accented characters directly...
Environment variables ShowEnvironment variables An environment variable is a dynamic value loaded into the memory that can be used by several processes operating simultaneously. On most operating systems, the location of some libraries or of the main system executables may...
Declaring a style sheet (CSS) ShowDeclaring a style sheet Style sheets are not directly integrated into the W3C's HTML recommendations. For this reason, you need to include elements in the HTML code indicating both the type of document, meaning the version of the HTML and CSS...