如何在我的 Coldfusion 页面中使用 Javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3558489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use Javascript in my Coldfusion page?
提问by Asim Zaidi
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
我有一个coldfusion页面,我是coldfusion的新手。我需要做的是在两者之间插入警报以查看时间。在 php 中,我可以关闭 php 标签并输入 javascript 标签并提醒该值。我将如何在冷融合中做到这一点?我有这个
<cfset right_now=Now()>
<cfscript>
alert(#right_now#);
</cfscript>
But its not working. thanks
但它不起作用。谢谢
回答by Daniel Vandersluis
<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.
<cfscript>是用于使用Coldfusion 脚本语言(又名 CFScript)的 Coldfusion 标签。如果您想使用 Javascript,请<script>像通常在 HTML 中一样打开一个标签。<cfoutput>如果您想在您的 javascript 中使用 Coldfusion 值,您可能需要确保它在标签内。
<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>
回答by Gary
You don't need to even use cfscript for this specific need. You could, for instance, do this:
您甚至不需要使用 cfscript 来满足此特定需求。例如,您可以这样做:
<script type="text/javascript">
var currtime = new Date();
alert(currtime);
</script>
回答by BIGDeutsch
... 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.
... 还有一点要记住,您不能直接从<cfscript>标签内输出 HTML 。但是,您可以通过从<cfscript>可以为您输出数据的标记中调用函数来解决此问题。
回答by Steven
Always remember the coldfusion begins and ends before anythingelse is executed: html, javaScript, sql, etc., so the javascript is getting an already formed code, which is CF instead of being hard coded.
永远记住,在执行其他任何东西之前,coldfusion 开始和结束:html、javaScript、sql 等,所以 javascript 得到一个已经形成的代码,这是 CF 而不是硬编码。

