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

Reply via email to