I am using jQuery 1.6.2 and ColdFusion 9.
When a page is requested, many files are included. Several files contain the jQuery document ready method. I want to set some global variables that I can use throughout the entire page. For example, I want to use these variables for my slides:
SlideUpRate = 400;
SlideDownRate = SlideUpRate * 2;
It seems that this works inconsistently. Is there a way to make it work consistently?
+++++++++++++++++++++++++++++++++++++++++++++++
Answer
In the index.cfm file, I set my global variables that can be used and reused in other jQuery throughout the rendered page.
<script type="text/javascript">
var SlideUpRate = 250;
var SlideDownRate = SlideUpRate * 2;
var HideRate = 250;
var ShowRate = HideRate * 2;
var ImageUnsaved = "layout/checkbox_unsaved.png";
var ImageSaved = "layout/checkbox_saved.png";
$(document).ready(function() {
// other jQuery stuff
});
Yes you can by adding them to the global namespace:
http://jsfiddle.net/QDPAm/
If you don’t want to pollute the global scope with multiply variables you can make an object to contain these variables:
And then in your
readyfunctions add variables to thevarsobject.And another
readyfunction:http://jsfiddle.net/aalouv/QDPAm/1/
I cant see why your example shouldn’t work. Maybe because your are trying to access some variables before they are set?
http://jsfiddle.net/aalouv/QDPAm/3/
Also creating variables without the var indicator first will add the variable to the global scope, So you can access the variable with:
windowor just without any namespace before.http://jsfiddle.net/aalouv/QDPAm/2/