How to call JavaScript function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmorand
    New Member
    • Sep 2007
    • 219

    How to call JavaScript function?

    I want to call a javascript function if my cfif logic is True. How do I call the javascript function? I just want to have a popup alert saying that the choice already exists, and then send the user back to the main page.

    [code=cfm]
    <cfif isDefined("Form .Add")>
    <cfquery name="validateN ame" datasource="Sec urity_Access" dbtype="ODBC">
    select sysID from tblSystem
    where name='#Form.nam e#'
    </cfquery>
    <cfif "validateName.R ecordCount" eq 1>
    <cfoutput>sysEx ists(#Form.name #)</cfoutput>
    <cflocation URL="system.cfm ">
    <cfelse>
    <cfquery name="insertSys tem" datasource="Sec urity_Access" dbtype="ODBC">
    insert into tblSystem (name,descripti on,updDateTime)
    values ('#name#','#des cription#','#Da teTime#')
    </cfquery>
    </cfif>
    </cfif>
    [/code]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Just add the code within script tags as you would in an HTML page:

    [HTML]<script type="text/javascript">
    // JS code ..
    </script>[/HTML]

    Comment

    • John Manoah

      #3
      JavaScript will be executed at the client side after the page is parsed by ColdFusion at the server side such that a code such as below is perfectly valid:

      Code:
      <script>
      alert('#variables.intInteger#');
      </script>
      When JavaScript reaches this line of code, the CF part would've already been replaced by the value it is assigned to.

      In your case you could simply write the JS piece along with the CF code.

      Comment

      • Ajo Koshy Josep
        New Member
        • Mar 2011
        • 5

        #4
        In the cfif logic where u want to get the alert and needs to re-direct, code like this

        Code:
        <cfif ... 
        
        <script type="text/javascript">
        alert("choice already exists");
        </script>
        
        <cflocation URL="system.cfm">
        
        <cfelse>
        
        <!------codes------>
        
        </cfif>
        Last edited by Niheel; May 5 '11, 03:00 PM. Reason: code tags please, thank you :)

        Comment

        Working...