Click to See Complete Forum and Search --> : Variables, objects...
pokemaniac
12-23-2002, 10:37 AM
Hello all! Thanks for taking the time to read me :) I'm trying to find a way to use a variable to reference an object.. I think. Here's the effect I want:
var strThing = "cookie"
document.strThing = "I have a cookie now."
Any way to do something like this? Thanks for any insight!
dotnetanimal
12-23-2002, 02:44 PM
var pagetitle = document.title;
document.write(pagetitle);
Charles
12-23-2002, 03:06 PM
This gets a bit confusing but in client side JavaScript var strThing = "cookie" sets a property of either the Window object, if it's a top level declaration, or of the Call object, if the declaration is inside a function declaration.
pokemaniac
12-23-2002, 03:44 PM
Hmm, so is that no? :) hehe I guess it comes down to: Can you use a document's cookie without saying cookie, or write a line without saying write etc...
Charles
12-23-2002, 03:54 PM
Oh, that's a different question than the one you seemed to be asking.
In JavaScript, and with a few exceptions, everything is just a property of some other object. And yes, you can assign away.
<script type="text/javascript">
<!--
p = document.write;
p('foo');
// -->
</script>
<script type="text/javascript">
<!--
document.write = 'foo';
alert(document.write);
// -->
</script>
pokemaniac
12-23-2002, 04:18 PM
Ok, yeah that's kind of it! But I'm still needing:
p='write';
document.p("alrighty");
They wouldn't be right next to each other, but that's the effect I need. So that I don't say document.write
Charles
12-23-2002, 04:34 PM
Objects just simply don't work that way. If you want to assign
document.write
to
document.p
then use
document.p = document.write.