View comments | RSS feed

JSStringFormat

Description

Escapes special JavaScript characters, such as single quotation mark, double quotation mark, and newline.

Return value

A string that is safe to use with JavaScript.

Category

String functions

Syntax

JSStringFormat(string)

Parameters

Parameter Description
string
A string or a variable that contains one.

Usage

Escapes special JavaScript characters, so you can put arbitrary strings safely into JavaScript.

Example

<!--- This example shows the use of the JSStringFormat function. ---->
<h3>JSStringFormat</h3>
<cfset stringValue = "An example string value with a tab chr(8), 
a newline (chr10) and some ""quoted"" 'text'">

<p>This is the string we have created:<br>
<cfoutput>#stringValue#</cfoutput>
</p>
<cfset jsStringValue = JSStringFormat(#stringValue#)>
<!----- Generate an alert from the JavaScript string jsStringValue. ---->
<SCRIPT>
s = "<cfoutput>#jsStringValue#</cfoutput>";
alert(s); 
</SCRIPT> 

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


Alexander Bunkenburg said on Sep 15, 2004 at 3:53 AM :
What does JSStringFormat do exactly?
" -> \"
' -> \'
TAB chr(8) -> \b
CR chr(13) -> \r
NL chr(10) -> \n
halL said on Sep 17, 2004 at 8:35 AM :
The list that Alexander Bunkenburg is not quite complete (and has the wrong ANSI code for TAB).
The following example shows all the characters that jsstringformat escapes:

<Cfoutput>
<Cfoutput>
" is \"<br>
#jsstringformat('"')#<br><br>
' is \'<br>
#jsstringformat("'")#<br><br>
\ is \\ <br>
#jsstringformat("\")#<br><br>
BS is \b<br>
#jsstringformat(chr(8))#<br><br>
TAB is \t<br>
#jsstringformat(chr(9))#<br><br>
NL is \n<br>
#jsstringformat(chr(10))#<br><br>
FF is \f<br>
#jsstringformat(chr(12))#<br><br>
CR is \r<br>
#jsstringformat(chr(13))#<br><br>
</CFOUTPUT></CFOUTPUT>

We should include the full information in the documentation for the next major ColdFusion release.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt1148.htm