Not yet rated

Problem

Showing message box using cfscript.

Solution

Used JavaScript inside the cfscript block.

Detailed explanation

The main scenario came into picture where the script has to be dynamically generated and written on the page which would use the value in a server side variable. The main implementation was to use the session variable value to do the check. Here in this example, a alert box is implemented.

ColdFusion does not have cfscript syntax to display a message. We have to use javascript to do the same. Let us consider the following snippet which is a classic CF check to display a message.,

Snippet #1

This can be implemented by the following approach in cfscript.,

<html>
      <head>
            <title>.:: WriteOutput - alert ::.</title>
            <cfif isdefined('btnSubmit')>
                  <script language="javascript" type="text/javascript">
                        alert('Page posted to the Server');
                  </script>
            </cfif>
      </head>
      <body>
            <cfform id="SampleForm" name="SampleForm" method="post">
                  <input type="submit" id="btnSubmit"
                        name="btnSubmit"
                        value="Click to Post the Page"/>
            </cfform>
      </body>
</html>

Snippet #2

<html>
      <head>
            <title>.:: WriteOutput - alert ::.</title>
            <cfscript>
                  if(isdefined('btnSubmit'))
                  {
                        writeoutput("<script language='javascript'
                                          type='text/javascript'>
                                          alert('Page posted to the Server');
                                          </script>");
                  }
            </cfscript>
      </head>
      <body>
            <cfform id="SampleForm" name="SampleForm" method="post">
                  <input type="submit" id="btnSubmit"
                        name="btnSubmit"
                        value="Click to Post the Page"/>
            </cfform>
      </body>
</html>

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes