Utility Function: JSClean()

This one I use a lot when writing out JSON. It probably is missing something that will still break a JS string, but its all I've found so far.


<cffunction name="JSClean" access="public" returntype="string" output="false">
    <cfargument name="sString" type="string" required="true" />
        
    <cfset var sReturn = REReplaceNoCase(Trim(arguments.sString), "\s", " ", "ALL") />
        
    <cfloop list="\:':"":&" delimiters=":" index="x">
        <cfset sReturn = ReplaceNoCase(sReturn, x, "\#x#", "ALL") />
    </cfloop>
        
    <cfreturn sReturn />
</cffunction>

 

Related Blog Entries

Comments

Jon in Chicago, July 2008

I'm Jon Hartmann and I'm a C# .Net developer by day, a ColdFusion guru by night, and all around Javascript fanatic. Stay right here to read my technical posts as I grapple with mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web. Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.