Search code examples
coldfusioncoldfusion-9coldfusion-8

CFAJAXPROXY is putting script into the head tag, not between the open and close head tags


We are using ColdFusion 8.

We are using CFAJAXPROXY. We just discovered that the tag that ColdFusion inserts in the head tag has an issue. It works fine on page that have a head tag like this:

<head>
    <title></title>
</head>

But, it does NOT work on pages where there is an ID in the head tag, like this:

<head id="SomeID">
    <title></title>
</head>

What happens is that CFJAXPROXY inserts the script inside the head tag, not withing the open and close head tags, which blows up the page, like this:

<head <script type="text/javascript"></script>
    <title></title>
</head>

The problem goes away immediately if the ID is removed from the head tag.

The question is whether this is a known CFAJAXPROXY bug? Is there a way to tell CFAJAXPROXY put the script within the head area but not in the head tag?


Solution

  • How are you inserting the <cfajaxproxy> into the page? Are you using a <cfinclude>, custom tag, or perhaps a method in Application.cfc? Anyway, a couple of things come to mind. If you're just putting the <cfajaxproxy> into an include file that already exists (i.e.:

    <html>
    <cfinclude template="stuff.cfm" />
    <head id="head1">
    </head>
    ...
    

    ), then perhaps you can insert a new <head> tag (without the id=) above the current one. I've tested and it works as far as <cfajaxproxy> is concerned, though I don't know what side effects it might have. It won't work if you put another <head> tag below the current one, or if you try to nest them or anything like that. What CF appears to be doing is looking for the first occurrence of <head, advancing one character, and then inserting the JS code. Another approach might be to use a phony tag including <head (this "works" as well):

    <fake<head>
    </fake<head>
    

    Of course, that tag is not legitimate XML so that could screw things up if you're parsing your templates using XMLParse() or anything like that.

    UPDATE: Here's another method you can use, which may work. I tried it but don't know if it will work with complex code. You would do the following in the onRequest() method of Application.cfc. I don't know what it would do to your processing time though:

    <cffunction name="onRequest" access="public" returntype="void" output="true">
        <cfargument name="target_page" type="string" required="true" />
    
        ...
    
        <cfsilent>
        <!--- Grab the requested page. --->
        <cfsavecontent variable="local.target_page">
            <cfinclude template="#arguments.target_page#" />
        </cfsavecontent>
    
        <!--- Replace bad <head> tag with good one --->
        <cfset local.target_page = REReplace(local.target_page, "(<head[^>]*>)", "<head>") />
        <cfajaxproxy ... />
        </cfsilent>
        <cfoutput>#local.target_page#</cfoutput>
    
        <cfreturn />
    </cffunction>