简体   繁体   English

在Coldfusion页面加载时执行javascript

[英]execute javascript as coldfusion page loads

I have a ColdFusion page that on it, has several cfinclude template calls which include separate files into the page. 我有一个ColdFusion页面,上面有几个cfinclude模板调用,这些调用将单独的文件包含到页面中。 I would like to update a javascript variable before each cfinclude template call. 我想在每个cfinclude模板调用之前更新一个javascript变量。 I've tried using: 我试过使用:

<script type="text/javascript">
myvariable = 'new status';
</script>

However, the javascript doesn't get executed until every single template included on the page finishes processing, instead of before each one executes. 但是,直到页面上包含的每个模板都完成处理,而不是在每个模板执行之前,JavaScript才会执行。

Is there some way I can actually execute javascript code AS the page loads? 有什么方法可以在页面加载时实际执行javascript代码吗?

You can accomplish this with <cfflush> . 您可以使用<cfflush>完成此<cfflush>

CFFlush will send the current HTML/javascript output to the browser as it continues to process. CFFlush将继续处理当前的HTML / javascript输出到浏览器。 It would look something like this: 它看起来像这样:

<CFInclude template="process1.cfm">
<CFoutput>
    <script type="text/javascript">
    myvariable = 'new status';
    </script>
</CFoutput>
<cfflush>
<CFInclude template="process2.cfm">

... and repeat. ...然后重复。

The javascript will be interpreted by the browser as soon as it is loaded. 加载后,浏览器将立即解释JavaScript。 This sometimes causes unpredictable behavior since the DOM is not complete and in a ready state, but for simple operations it works. 有时这会导致无法预测的行为,因为DOM尚未完成并且处于就绪状态,但是对于简单的操作它就可以工作。

Edit: note that you often have to pad your output with something like <cfoutput>#repeatString(" ", 250)#</cfoutput> before the browser will process it. 编辑:请注意,在浏览器处理输出之前,通常必须使用<cfoutput>#repeatString(" ", 250)#</cfoutput>类的内容填充输出。 See http://www.raymondcamden.com/index.cfm/2006/11/29/A-Loading-page-with-CFFLUSH-and-JavaScript 参见http://www.raymondcamden.com/index.cfm/2006/11/29/A-Loading-page-with-CFFLUSH-and-JavaScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM