Asim Zaidi
Asim Zaidi

Reputation: 28284

How do I use Javascript in my Coldfusion page?

I have a coldfusion page and I am very newbie in coldfusion. What I need to do is to insert the alert in between to see what is the time. In php I could close the php tags and enter the javascript tag and alert out the value. How would I do that in coldfusion? I have this

<cfset right_now=Now()> 
        <cfscript>
    alert(#right_now#);
    </cfscript>

But its not working. thanks

Upvotes: 8

Views: 24956

Answers (4)

Steven
Steven

Reputation: 21

Always remember the coldfusion begins and ends before anything else is executed: html, javaScript, sql, etc., so the javascript is getting an already formed code, which is CF instead of being hard coded.

Upvotes: 2

Gary
Gary

Reputation: 453

You don't need to even use cfscript for this specific need. You could, for instance, do this:

<script type="text/javascript">
     var currtime = new Date();
     alert(currtime);
</script>

Upvotes: 6

BIGDeutsch
BIGDeutsch

Reputation: 103

... Also a point to remember, you can't directly output HTML from within a <cfscript> tag. You can however get around this by calling a function from within a <cfscript> tag that can output the data for you.

Upvotes: 3

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94103

<cfscript> is a Coldfusion tag for using the Coldfusion scripting language (aka CFScript). If you want to use Javascript, open a <script> tag like you would normally in HTML. You'll probably want to make sure it's inside a <cfoutput> tag if you want to use Coldfusion values within your javascript.

<cfset right_now = Now()>

<cfoutput>
<script type="text/javascript">
  alert('#right_now#'); // don't forget you need to put quotes around strings in JS
</script>
</cfoutput>

Upvotes: 19

Related Questions