{"id":240,"date":"2012-03-23T10:53:46","date_gmt":"2012-03-23T14:53:46","guid":{"rendered":"http:\/\/www.thefaberfamily.org\/search-smith\/?p=240"},"modified":"2012-03-23T20:58:45","modified_gmt":"2012-03-24T00:58:45","slug":"coldfusion-cfajaxproxy-and-the-head-tag","status":"publish","type":"post","link":"http:\/\/www.thefaberfamily.org\/search-smith\/2012\/03\/coldfusion-cfajaxproxy-and-the-head-tag\/","title":{"rendered":"ColdFusion: CFAJAXPROXY and the HEAD tag"},"content":{"rendered":"<p>A developer had an interesting problem recently which he <a title=\"CFAJAXPROXY is putting script into the head tag, not between the open and close head tags\" href=\"http:\/\/stackoverflow.com\/questions\/9807082\/cfajaxproxy-is-putting-script-into-the-head-tag-not-between-the-open-and-close\" target=\"_blank\">related on StackOverflow<\/a>. Normally, if the <code>&lt;cfajaxproxy&gt;<\/code> tag is used it inserts JavaScript code immediately after the <code>&lt;head&gt;<\/code> tag. However, if the <code>&lt;head&gt;<\/code> tag has attributes, then ColdFusion tries to insert the JavaScript code <em>within<\/em> the <code>&lt;head&gt;<\/code> tag (i.e., before the <code>&gt;<\/code> sign marking the end of the tag):<\/p>\n<pre>&lt;html&gt;\r\n&lt;head &lt;script type=\"text\/javascript\"&gt;\/* &lt;![CDATA[ *\/_cf_loadingtexthtml=\"&lt;img alt=' ' src='\/CFIDE\/scripts\/ajax\/resources\/cf\/images\/loading.gif'\/&gt;\";\r\n_cf_contextpath=\"\";\r\n_cf_ajaxscriptsrc=\"\/CFIDE\/scripts\/ajax\";\r\n_cf_jsonprefix='\/\/';\r\n_cf_clientid='A0A76C2B1D80B084CEAB5AA874B0CEFE';\/* ]]&gt; *\/&lt;\/script&gt;&lt;script type=\"text\/javascript\" src=\"\/CFIDE\/scripts\/ajax\/messages\/cfmessage.js\"&gt;&lt;\/script&gt;\r\n&lt;script type=\"text\/javascript\" src=\"\/CFIDE\/scripts\/ajax\/package\/cfajax.js\"&gt;&lt;\/script&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\/* &lt;![CDATA[ *\/\r\nColdFusion.Ajax.importTag('CFAJAXPROXY');\r\n\/* ]]&gt; *\/&lt;\/script&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\/* &lt;![CDATA[ *\/\r\nvar _cf_article=ColdFusion.AjaxProxy.init('\/test\/stuff.cfc','stuff');\r\n\/* ]]&gt; *\/&lt;\/script&gt;\r\nprofile=\"http:\/\/gmpg.org\/xfn\/11\"&gt;\r\n&lt;title&gt;Test Page&lt;\/title&gt;\r\n&lt;\/head&gt;<\/pre>\n<p>At first, I thought this was merely an inconvenience &#8211; the developer in question had stated that he was using the <code>id<\/code> attribute of the &lt;head&gt; tag, which is not valid HTML 4.01 syntax. However, it <em>is<\/em> valid HTML5 syntax and, what&#8217;s more, it doesn&#8217;t work properly even <em>with<\/em> valid HTML 4.01 attributes for the <code>&lt;head&gt;<\/code> tag, as seen in the code example above. The <code>profile<\/code> attribute <em>is<\/em> valid HTML 4.01 syntax and there is no reason that ColdFusion should not detect the attribute. What ColdFusion appears to be doing is searching for the first occurrence of <code>&lt;head<\/code>, advancing one character (no matter what that character is), and then inserting the JavaScript code. That strikes me as pretty poor programming. The <code>&lt;cfajaxproxy&gt;<\/code> tag should either search for a regular expression (<code>\"(&lt;head&gt;|&lt;head\\s[^&gt;]*&gt;)\"<\/code>) and insert the code after the match, or traverse the elements of the page&#8217;s DOM tree. I now have no qualms with stating that this is a bug. (The &lt;cfajaxproxy&gt; tag was introduced in ColdFusion 8; the bug exists in at least ColdFusion 8 and ColdFusion 9 &#8211; I don&#8217;t know about ColdFusion 10.)<\/p>\n<p>All that said, how do we work around this issue? Fixing tons of web pages certainly isn&#8217;t convenient, but there may be some options. One, you can put in an additional <code>&lt;head&gt;<\/code> tag (with its closing tag) above the &#8220;problematic&#8221; one. I have no idea what sort of side effects this may produce. Two, you can put a set of phony tags above the problematic one: <code>&lt;fake&lt;head&gt;&lt;\/fake&lt;head&gt;<\/code>. Again, I have no idea what side effects this may produce. Your JavaScript code certainly won&#8217;t be in the <code>&lt;head&gt;<\/code> tag if you take this route.<\/p>\n<p>Third, you can add some code into the <code>onRequest()<\/code> method of your Application.cfc file (yes, there are issues with web services when using <code>onRequest()<\/code> but <a title=\"Re-Opening Web Services in Coldfusion using Application.cfc\" href=\"http:\/\/creativenotice.com\/2010\/04\/re-opening-web-services-in-coldfusion-using-application-cfc\/\" target=\"_blank\">those can be addressed<\/a>):<\/p>\n<pre>&lt;cfsilent&gt;\r\n&lt;!--- Grab the requested page. ---&gt;\r\n&lt;cfsavecontent variable=\"local.target_page\"&gt;\r\n&lt;cfinclude template=\"#arguments.target_page#\" \/&gt;\r\n&lt;\/cfsavecontent&gt;\r\n\r\n&lt;cfset local.target_page = REReplace(local.target_page, \"(&lt;head\\s[^&gt;]*&gt;)\", \"&lt;head&gt;\") \/&gt;\r\n&lt;cfajaxproxy cfc=\"test.stuff\" jsclassname=\"stuff\" \/&gt;\r\n&lt;\/cfsilent&gt;\r\n&lt;cfoutput&gt;#local.target_page#&lt;\/cfoutput&gt;<\/pre>\n<p>Oddly, the above works even when the call to <code>&lt;cfajaxproxy&gt;<\/code> is on the target page instead of in <code>onRequest()<\/code>! I guess that tag is not actually invoked until the page is actually served. I don&#8217;t know how the above will affect the performance of the page, and I don&#8217;t know if it will even work with complex code (I tried it with a rather uncomplicated page myself). It does appear to work at first glance, but it&#8217;s a workaround. The real issue is that there is a bug.<\/p>\n<p><strong>Update:<\/strong> <a title=\"Bug #3146932 - CFAJAXPROXY and the HEAD tag\" href=\"https:\/\/bugbase.adobe.com\/index.cfm?event=bug&amp;id=3146932\" target=\"_blank\">Bug reported to Adobe<\/a>.<\/p>\n<div class=\"simple_likebuttons_container_small\">\r\n      <div class=\"simple_likebuttons_googleplus\">\r\n        <g:plusone size=\"medium\" count=\"false\" href=\"http:\/\/www.thefaberfamily.org\/search-smith\/2012\/03\/coldfusion-cfajaxproxy-and-the-head-tag\/\"><\/g:plusone>\r\n      <\/div>\r\n    \r\n      <div class=\"simple_likebuttons_twitter simple_likebuttons_twitter_s\">\r\n        <a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-count=\"none\" data-url=\"http:\/\/www.thefaberfamily.org\/search-smith\/2012\/03\/coldfusion-cfajaxproxy-and-the-head-tag\/\" data-lang=\"en\">Tweet<\/a>\r\n      <\/div>\r\n    \r\n      <div class=\"simple_likebuttons_facebook\">\r\n        <div id=\"fb-root\"><\/div>\r\n        <script>(function(d, s, id) {\r\n          var js, fjs = d.getElementsByTagName(s)[0];\r\n          if (d.getElementById(id)) {return;}\r\n          js = d.createElement(s); js.id = id;\r\n          js.src = \"\/\/connect.facebook.net\/en_US\/all.js#xfbml=1\";\r\n          fjs.parentNode.insertBefore(js, fjs);\r\n        }(document, \"script\", \"facebook-jssdk\"));<\/script>\r\n        <div class=\"fb-like\" data-href=\"http:\/\/www.thefaberfamily.org\/search-smith\/2012\/03\/coldfusion-cfajaxproxy-and-the-head-tag\/\" data-send=\"false\" data-layout=\"button_count\" data-show-faces=\"false\" data-width=\"90\"><\/div>\r\n      <\/div>\r\n    <\/div>","protected":false},"excerpt":{"rendered":"<p>A developer had an interesting problem recently which he related on StackOverflow. Normally, if the &lt;cfajaxproxy&gt; tag is used it inserts JavaScript code immediately after the &lt;head&gt; tag. However, if the &lt;head&gt; tag has attributes, then ColdFusion tries to insert the JavaScript code within the &lt;head&gt; tag (i.e., before the &gt; sign marking the end [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[74,73,87],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-coldfusion","tag-bug","tag-cfajaxproxy","tag-coldfusion"],"_links":{"self":[{"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":4,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":242,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/posts\/240\/revisions\/242"}],"wp:attachment":[{"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thefaberfamily.org\/search-smith\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}