Basically, I was just trying to set a variable once on a page and then be able to refer to it anywhere, anytime, apart from having to run additional scripts to place the variable in an id or get the value from another script.
It just seems like everytime I want to access a variable somewhere, I have to create a script to move into a location for access. Unlike, for instance, session variables in ColdFusion where I can write <cfset session.storyID = '#storyID#'> and then access session.storyID anywhere I want in the entire application, even across pages. It just gets tedious having to move javascript variables inside the () from one function call to the function, or from an ajax response variable to an id, etc. I'm used to using global variables, such as session variables and using namespacing to keep them separate. I just didn't know if javascript/jQuery had anything like that or not. It seemed like global variables were what I was looking for, but apparently they're meant to be used inside other script sections, rather than out in the HTML, as we do in jQuery. Maybe that makes some sense. I'm just exploring alternative and experimenting. Rick -----Original Message----- From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On Behalf Of Eric Garside Sent: Tuesday, April 07, 2009 4:00 PM To: jQuery (English) Subject: [jQuery] Re: How do I access global variables for id's, etc.? What are you actually trying to achieve here? Through all the code posted, I'm still a bit unclear on the actual goal you're trying to achieve. On Apr 7, 3:20 pm, Jonathan <jdd...@gmail.com> wrote: > I know global variables seem convenient but they are really quite > evil. Once your project grows to even a moderate size they become a > nightmare to deal with violate a number of good design tenants, you > lose any modularity the code might have had and will run into > namespace conflicts (No matter how detailed you may name them). > > jQuery and JavaScript aren't a templating system, trying to make them > work like one will just be frustrating > > On Apr 7, 11:52 am, "Rick Faircloth" <r...@whitestonemedia.com> wrote: > > > Actually, I didn't get what I thought. > > > The various sections of code without line breaks were bumping into each > > > other making it look like the code was working! Sorry! > > > This doesn't work: <p>gNewStoryID = <script>gNewStoryID</script></p> > > > It would be nice if I could just stick a "$" on the front of a string and > > have > > > jQuery parse it as a variable; id="$gNewStoryID". That would be as > > convenient > > > as session variables in ColdFusion! Define once, user anywhere! > > > No more extra functions just to change an attribute to a different value. > > > Rick > > > From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On > > Behalf Of Rick Faircloth > > Sent: Tuesday, April 07, 2009 2:18 PM > > To: jquery-en@googlegroups.com > > Subject: [jQuery] Re: How do I access global variables for id's, etc.? > > > Surprisingly, it did work. > > > I ran this code on an otherwise blank page (no doctype, etc.): > > > <script type="text/javascript" src="jquery.js"></script> > > > <script> > > > var gNewStoryID = 2 > > > </script> > > > <p>This is a paragraph of text. Inside this text I want to embed the value > > > of the global variable gNewStoryID, which is > > <script>document.write(gNewStoryID);</script>, isn't it?</p> > > > And, in FF 3, I get: > > > This is a paragraph of text. Inside this text I want to embed the value > > > of the global variable gNewStoryID, which is 2, isn't it? > > > So, there you go! > > > Concerning the alternative. yes, I realize that the typical way of doing > > this "insertion" > > > is to use JS to generate the values or HTML etc., and I have been doing that > > with jQuery. > > > I thought I would just see if there wasn't a way to "free" some of these > > variables from > > > the constant manipulation via JS. > > > Sure seems like JS could benefit from some way to refer to the value of > > these variables > > > apart from simply writing more code. > > > But let me know if you get that first example above to work for you. The > > examples of > > > how to inject HTML with the variables are fine, but they're actually more > > code than > > > just a line of jQuery inside a function. > > > I'm just trying to figure out ways to cut down on the amount of code that > > has to > > > clutter up a page. > > > Rick > > > From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On > > Behalf Of Michael Geary > > Sent: Tuesday, April 07, 2009 1:23 PM > > To: jquery-en@googlegroups.com > > Subject: [jQuery] Re: How do I access global variables for id's, etc.? > > > > I'm able to output a global variable value like this: > > > <p>The value of myGlobalVariable is <script>myGlobalVariable</script>.</p> > > > Huh? What browser does that work in? HTML isn't a templating language, and a > > <script> tag doesn't do a text replacement of the script's return value, it > > just runs the script. You didn't actually get this to work, did you? > > > > I would like to be able to do something as simple as > > > <p id="<script>myGlobalVariable</script>">xyz</p>, > > > but apparently the "" marks are a problem. > > > Whoa cowboy, that's even farther from anything you could ever actually do: > > you're trying to nest an HTML tag inside the attribute of another tag. > > > Instead, the way you do stuff like this is to write JavaScript code that > > generates the HTML or DOM elements. > > > For example, during page loading (not in a document ready function), you can > > use document.write(): > > > <script type="text/javascript"> > > // myGlobalVariable has been previously defined > > document.write( '<p id="', myGlobalVariable, '">xyz</p>' ); > > </script> > > > That works with local variables as well, of course. > > > <script type="text/javascript"> > > (function() { > > var foo = someFunction(); > > document.write( '<p id="', foo, '">xyz</p>' ); > > })(); > > </script> > > > Or, in jQuery you can do things like this: > > > <script type="text/javascript"> > > $(function() { > > $('#someContainer').html( > > $('<p>xyz</p>').attr({ id: myGlobalVariable }) > > ); > > }); > > </script> > > > Alternatively, there are several JavaScript-based template systems, from the > > very simple to the rather complex. They may let you code in a style closer > > to what you're hoping to use. > > > -Mike > > > _____ > > > From: Rick Faircloth > > > I would like to be able to do something as simple as > > > <p id="<script>myGlobalVariable</script>">xyz</p>, > > > but apparently the "" marks are a problem. > > > I'm able to output a global variable value like this: > > > <p>The value of myGlobalVariable is <script>myGlobalVariable</script>.</p> > > > Is there some way to use global variable values with an id attribute? > > > Thanks, > > > Rick > > > ---------------------------------------------------------------------------- > > ----------------------------------------------------------- > > > "It has been my experience that most bad government is the result of too > > much government." - Thomas Jefferson