Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make my own JSStringFormat for 4.01 1

Status
Not open for further replies.

simmerdown

Programmer
Jun 6, 2000
100
US
I've got CF5 on my local developement machine, 4.01 on the webserver, and the software department says "no upgrade to CFServer for at least a month or two." Thus my current situation of developing something that works fine locally, alerts me to breaks on the web server, and pinches me betwixt a deadline and my amateur coding abilities.

All I need (at the moment) is CF-4.5-and-up's [COLOR=cc0000]JSStringFormat[/color] function. Since I don't have CF 4.5 or newer, is there another way to escape everything I need to escape for JavaScript to handle strings I throw at it? Put another way, what do I need to escape, and how do I do it? How do I do everything that JSStringFormat would do for me?

I'm sure someone out there ran into (and solved) this difficulty before 4.5 was released and MM made everything easy. Please help!
 
I haven't done this, but it doesn't seem all that mysterious. All we need to know is what characters need to be escaped to make them Javascript-safe, and how to escape a character.

The latter is easy: just precede a character with a backslash. The CF 5.0 documentation refers to "special JavaScript characters, such as the single quote ('), double quotes ("), and newline character. The implication is that there are other characters that need escaping, but if you handle the quotes and newline, you should be OK. If you find that your application needs another character escaped, then you can modify the code to handle it.

Since you can't write a function in CF4.01, use a custom tag or a CFX tag. The CFX tag would be better because I can't figure out how to make ColdFusion's regular expressions match a newline. Nevertheless, the following custom tag will escape single and double quotes:

Code:
<!--- Custom Tag: escapeQuotes.cfm --->
<cfif not IsDefined(&quot;attributes.string&quot;)>
   You must supply the STRING attribute<BR>
   <cfabort>
</cfif>
<cfif not IsDefined(&quot;attributes.returnvar&quot;)>
   You must supply the RETURNVAR attribute<BR>
   <cfabort>
</cfif>

<cfset str = ATTRIBUTES.string>
<cfset str = REreplace(str, &quot;'&quot;, &quot;\'&quot;, &quot;all&quot;)>
<cfset str = REreplace(str, '&quot;', '\&quot;', &quot;all&quot;)>

<cfset &quot;caller.#attributes.returnvar#&quot; = str>

And here is a test script that uses it:

Code:
<cfset mystring = &quot;This really isn't &quot;&quot;fair&quot;&quot;&quot;>
<cf_escapeQuotes string=&quot;#mystring#&quot; returnvar=&quot;safe_string&quot;>

<cfoutput>
Original string:
<pre>#mystring#</pre>
Escaped string:
<pre>#safe_string#</pre>

Writen by Javascript:
<script language=&quot;javascript&quot;>
	document.write('<pre>' + '#safe_string#' + '\n</pre>');
</script>
</cfoutput>

As I said, if someone can figure out how to match a newline with a regular expression, then a custom tag will work. Otherwise, I think that you will have to write a CFX tag.
 
You can replace newline characters by adding this line to the custom tag after the other <cfset> statements:
Code:
  <cfset str = REReplace(str,&quot;[#Chr(10)##Chr(13)#]{1,2}&quot;,&quot;\n&quot;,&quot;all&quot;)>
 
That's very good. I had tried assigning chr(13) & chr(10) to a variable and using the variable as the regexp. Even if that has worked, I would have needed to define the end-of-line character(s) differently for different platforms. Your solution is brilliant! It works on UNIX, Macintosh and Windows. And for those reading who may not see it: the only technical error (and it is so minor as to be insignificant) is that the regexp will match a single chr(13) or chr(10) on a Windows machine and interpret it as an end-of-line. But, it is highly unlikely that one would encounter that in a text file on a windows machine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top