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. 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