admin管理员组

文章数量:1406177

I have often seen that sometimes dynamic files are served through the <script> tag like below. The "abc.cfm" file contains both JavaScript and server side CF code.

<script type="text/javascript" src="abc.cfm">

Normally JavaScript files are cached in the browser if we use a simple .js file, rather than a server .cfm file. So how will the above behave with regard to caching? Will the browser use the cached contents of JavaScript, or it will make a call every time to the CF server to fetch the contents of the code and repile the js again? Also, what are the advantages and disadvantages of using this technique?

I searched a lot but could not find a relevant good answer which explains this properly.

Regards

I have often seen that sometimes dynamic files are served through the <script> tag like below. The "abc.cfm" file contains both JavaScript and server side CF code.

<script type="text/javascript" src="abc.cfm">

Normally JavaScript files are cached in the browser if we use a simple .js file, rather than a server .cfm file. So how will the above behave with regard to caching? Will the browser use the cached contents of JavaScript, or it will make a call every time to the CF server to fetch the contents of the code and repile the js again? Also, what are the advantages and disadvantages of using this technique?

I searched a lot but could not find a relevant good answer which explains this properly.

Regards

Share Improve this question edited Dec 7, 2013 at 21:33 Leigh 28.9k10 gold badges57 silver badges109 bronze badges asked Dec 7, 2013 at 19:17 RegualRegual 3771 gold badge5 silver badges21 bronze badges 1
  • FYI: CSS can be served via ColdFusion too – James A Mohler Commented Dec 7, 2013 at 21:33
Add a ment  | 

1 Answer 1

Reset to default 10

The advantage of the technique above is that the server is generating JavaScript. That means you can use ColdFusion to perhaps get data from a database or the user's session. Perhaps you need a set of data to populate some dropdowns, depending on what someone selects. The abc.cfm in the example above can query the DB and generate some JavaScript containing the data you need.

With regards to the browser caching, the browser will cache what it's told to cache. If you send headers in the abc.cfm telling the browser not to cache the file, it'll reload it each time the page is loaded. A typical set of headers used to indicate no caching are:

<cfheader name="Cache-Control" value="no-cache, must-revalidate">
<cfheader name="Pragma"value="no-cache">
<cfheader name="Expires" value="Sat, 26 Jul 1997 05:00:00 GMT"> <!--- Date in the past --->

If you pop that at the top of your ABC.cfm, the browser ought to load it afresh each time.

The downsides of the approach are that the browser has to load the abc.cfm each time the page loads, slowing the page down. Another problem is that the abc.cfm tends to contain a mix of JS and CF code, which is not nice to maintain. An alternative would be to write all your code as a .JS file, which you let the browser cache and to use CF code to just spit out the data you need as JSON direct into the page. You then call the JS code, passing in the JSON data you printed into the page. Look at SerializeJSON for a neat way to turn CF data into JSON the client can read.

本文标签: javascriptServing cfm files with ltscriptgt tagStack Overflow