Skip to main content
May 12, 2009
Question

javascript functions inside cfloop

  • May 12, 2009
  • 3 replies
  • 3315 views

Hi All,

I need to call a javascript function inside cfloop i.e for every iteration it should call a javascript function. Please help me in getting the solution to this.

    This topic has been closed for replies.

    3 replies

    Inspiring
    May 12, 2009

    <body>
    hello
    <cfflush>
    <cfoutput>
    <cfloop from="1" to="3" index="i">
    <script>
    document.write("#i#");
    </script>

    </cfloop>
    </cfoutput>
    </body>

    ilssac
    Inspiring
    May 12, 2009

    While this does show a JavaScript function inside a ColdFusion loop combined with a <cffush...> tag to stream the response to the client, within some very well defined limitations.

    It does not show JavaScript and ColdFusion sharing.  This is a simple example of using CFML to build JavaScript which can be a very powerful tool set.  But one should clearly understand the processing here.

    1) ColdFusion iterates over a loop building output like this:

    <script>

      document.write("1");

    </script>

    <script>

      document.write("2");

    </script>

    <script>

      document.write("3");

    </script>

    2) ColdFusion returns that output to the web server, incrementally if running after a <cfflush...>

    3) The web server returns the output to the client

    4) The client process the script and writes the strings "1", "2", and "3" to the document.

    To extend that example, this would not work:

    <body>
    hello
    <cfflush>
    <cfoutput>

    <script>

      var jsVar = 0;

    </script>

    <cfloop from="1" to="3" index="i">
    <script>
    document.write("#i#");

    jsVar += #i#;
    </script>

    </cfloop>

    jsVar = #jsVar#
    </cfoutput>
    </body>

    Message was edited by: Ian Skinner

    ilssac
    Inspiring
    May 12, 2009

    Javascript runs on the client AFTER the CFML is completely done and has moved on to an entirely different request.

    You probably can't do what you think you want to do, but since we have little idea what you are actually trying to accomplish we can offer much more then then the definition deferences between server processes and client processes.

    Inspiring
    May 12, 2009

    Since js runs on the client and cf runs on the server, it might not be possible.  What will this js function actually do?

    May 12, 2009

    Hi Dan,

    I have a cfloop statement which loops through a structure and every object in that structure should be serialized using the serializer() method (wffxserializer)

    Thank you in advance

    ilssac
    Inspiring
    May 12, 2009

    What type of structure?  What type of objects?  ColdFusion or Javascript?

    If the former look at <cfwddx....> tag.

    If the latter you need to look at code that runs on the client. I.E JavaScript loops and serializers.

    I presume you mean 'wddx' when you typed 'wffx'.