Tuesday, June 8, 2010

CFLocation in CFScript

I often find myself converting old, long blocks of CFML to CFScript before attempting to refactor, modularize and patternize that code. I do this both to make the code more readable for me and to make the code more portable since much of my refactoring will be done in CFScript anyway. Unfortunately CFScript doesn't support all of the same functions provided for in CFML--in fact, CFScript functions are a very limited set unless you start digging into the Java functions and how to access them in Coldfusion. Yesterday I ran into a need to have CFLocation in my CFScript. It took me a while but I found a really old blog entry with the following solution:
GetPageContext().getResponse().sendRedirect([URL String]);
It works just as I need it to. I haven't yet done the research but I'm assuming this is an example of accessing the Java side of CF. It will be interesting to explore the three functions AND how, apparently, Java supports chaining similar to the jQuery does.

5 comments:

  1. Here is where I originally found this solution. See the "answer" at the bottom of the page:
    http://www.experts-exchange.com/Software/Server_Software/Web_Servers/ColdFusion/Q_21348909.html

    ReplyDelete
  2. and here is an exhaustive document of GetPageContext(), including the example I have used, at Ben Nadels blog:
    http://www.bennadel.com/blog/758-ColdFusion-GetPageContext-Massive-Exploration.htm

    ReplyDelete
  3. Is this as simple as:
    <cfscript>
    GetPageContext().getResponse().sendRedirect("http://www.google.com")
    </cfscript>

    I am getting an error on above:
    The start tag must have a matching end tag. An explicit end tag can be provided by adding .

    ReplyDelete
  4. @PuneetSays - I think you're just missing a statement terminator, ";", after the sendRedirect() call. CfScript is a little more strict than Javascript where all statements must be terminated. Here is another example and I will edit my blog entry to clarify (note in teh next example the handy "writeOutput() method which is the same as CFOutput):


    foo = "http://codebuoy.blogspot.com";
    writeOutput(foo);
    GetPageContext().getResponse().sendRedirect(foo);

    ReplyDelete