How to use Javascript on my Coldfusion page?

I have a coldfusion page and am very new to coldfusion. What I need to do is insert an alert between them to see what time it is. In php, I could close the php tags and enter the javascript tag and warn the value. How do I do this when cold? I have it

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

But it does not work. thanks

+7
javascript coldfusion
source share
4 answers

<cfscript> is a Coldfusion tag for using the Coldfusion scripting language (aka CFScript). If you want to use Javascript, open the <script> , as usual in HTML. You will probably want to make sure that it is inside the <cfoutput> tag if you want to use Coldfusion values ​​in 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> 
+18
source share

You do not even need to use cfscript for this particular need. You could, for example, do this:

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

... It is also important to remember that you cannot directly output HTML from the <cfscript> . However, you can get around this by calling a function from the <cfscript> , which can output data for you.

+3
source share

Always remember that coldfusion starts and ends before anything else runs: html, javaScript, sql, etc., so javascript gets already generated code that is CF instead of hard coding.

+2
source share

All Articles