I assume you are not using ColdFusion 8 as its AJAX proxy handles loading your _javascript_ into the DOM for you.  If you are, we should have a different conversation.

Ok, for your eval to work, you have to pull the _javascript_ out of your page content, and eval it to get it into the DOM.  If your _javascript_ functions are written function {functionName} ({functionArgs}) you'll need to switch them to window.{functionName} = function {functionArgs}

I've included a function I used for the purpose you described.  It was written before I started using jQuery, so there could be a jQuery version of this already in existance somewhere, but it gives you the general idea.  JS functions in the code I worked with were created as follows: function {functionName}({args}) with no html comments within the _javascript_.  If you have comments in the _javascript_ you will get unpredicatable results. I've meant to go back and add some code to strip out any comments but I never got around to it.  If you have either _javascript_ or HTML comments in your JS you will probably need to add in some regex to strip them out before you eval.  This includes the <!-- //--> comments that were often wrapped around inline _javascript_ blocks.  After I got my _javascript_ content back (and decoded the JSON), I ran launchJavascript({contentHTML}) to load it into the DOM.

The function below rewrites functions written function {functionName} ({functionArgs}) to read window.{functionName} = function ({functionArgs}) Then it evals the new string of _javascript_ which loads it into the DOM.  

function launchJavascript(content) {

try {
var findScripts = /(?:<script.*?>)((?:\s|.)*?)(?:<\/script>)/img;
var findScriptContent = /(<script.*?>)((?:\s|.)*?)(<\/script>)/im;
var findFunctionInfo = /(function)((?:\s)+)((?:\w)+)((?:\s)*)(\((?:\s|.)*?\))/img;
//Create an array containing all script tags (with content) found in the supplied string
var scriptContent = content.match(findScripts);
if(scriptContent) 
{
   for(var s = 0; s < scriptContent.length; s++) 
{
   var thisScript = scriptContent[s];
var scriptText = thisScript.match(findScriptContent)[2];
var findFunc = scriptText.replace(findFunctionInfo,"window.$3 = $1$5");
//js = js + findFunc;
if(findFunc != null && findFunc != '')
{
eval(findFunc);
}
}
}
} catch(e) 
{
alert(e);

} // End catch
} // end function


Reply via email to