Search

Tuesday, July 24, 2007

Execute JavaScript with cfSnippetExecutor

This might be a duh! for some but thought I'd blog about it anyway. You can execute JavaScript as well as CF with the cfSnippetExecutor. Here is an example:
<-- Create a textarea for the results to be displayed -->
<textarea id="results" cols="80"></textarea>
<script type="text/javascript">
var htmlString = "<span>test</span>";
// Replace the < and > with their safe counterparts
htmlString = htmlString.replace(/</g, "&amp;lt;");
htmlString = htmlString.replace(/>/g, "&amp;gt;");

// Display the results
document.getElementById('results').innerHTML = htmlString;
</script>
<--- Same code as above but in ColdFusion --->
<cfset html = "<span>test</span>" />
<cfset html = html.replaceall("<", "&amp;lt;") />
<cfset html = html.replaceall(">", "&amp;gt;") />
<cfoutput>
<textarea cols="80">#html#</textarea>
</cfoutput>
// //]]>