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 Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about 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.