Ask Ben: Executing ColdFusion Custom Tag Code If First Run Only

<!--- Turn on explicit output. --->
<cfsetting enablecfoutputonly="true" />
 
<!---
	Param the tag attribute for our name space. To make sure
	that the meta data that we store with this tag does not
	conflict with other page variables, we will create namespace
	for storage. This name space can be overridden by the
	calling code.
--->
<cfparam
	name="ATTRIBUTES.NameSpace"
	type="variablename"
	default="REQUEST.CustomTagNameSpaces.Demo"
	/>
 
 
<!---
	Check to see if the namespace for this tag exists yet.
	If not, we will create a default version.
--->
<cfif NOT StructKeyExists( CALLER, ATTRIBUTES.NameSpace )>
 
	<!--- Create default name space for this tag. --->
	<cfset CALLER[ ATTRIBUTES.NameSpace ] = {
		JavaScriptRendered = false,
		TagCount = 0
		} />
 
</cfif>
 
 
<!---
	ASSERT: At this point, we can access the name space for
	this tag (even if it was just created).
--->
 
 
<!--- Check to see if the Javascript has been rendered. --->
<cfif NOT CALLER[ ATTRIBUTES.NameSpace ].JavaScriptRendered>
 
	<!--- Render Javascript. --->
	<cfoutput>
		<script type="text/javascript">
			// Script would go here.
		</script>
	</cfoutput>
 
	<!--- Flag script as being rendered. --->
	<cfset CALLER[ ATTRIBUTES.NameSpace ].JavaScriptRendered = true />
 
</cfif>
 
 
<!---
	ASSERT: At this point, we know that the Javascript has
	either been rendered by this tag or a previous tag.
--->
 
 
<!--- Render tag content. --->
<cfoutput>
	Tag #++CALLER[ ATTRIBUTES.NameSpace ].TagCount#<br />
</cfoutput>
 
 
<!--- Turn off explicit output. --->
<cfsetting enablecfoutputonly="false" />
 
<!--- Exit tag. --->
<cfexit method="exittag" />

For Cut-and-Paste