PDA

View Full Version : text variable in syntax



dukevn
07-15-2008, 11:21 PM
Hi folks,

I have just basic knowledge in javascript, so sorry if I do not even know how to ask correctly :). I wonder that I can control the javascript syntax by text variable or not. For example, for the below code:

object.style.left="20px"
I want to control the direction, so I change it to

var direction='left'
...
object.style.direction="20px"
Is it correct, or it is impossible to do that?

Thanks,

D.

clueful
07-16-2008, 12:29 AM
for the below code:

object.style.left="20px"
I want to control the direction, so I change it to

var direction='left'
...
object.style.direction="20px"
Is it correct, or it is impossible to do that?

Thanks,

D.Try
var direction='left';

object.style[direction]="20px"

dukevn
07-16-2008, 01:00 AM
Try
var direction='left';

object.style[direction]="20px"

Hi Clueful,

Thanks for your reply. What if I want to control the "style" text? Can it be

var prop='style';

object[prop].left="20px"
and if I want to control both properties

var prop='style';

var direction='left'

object[prop][direction]="20px"

Is it correct?

Thanks,

D.

jscheuer1
07-16-2008, 07:37 AM
It can come in handy in certain situations to define and bracket properties, particularly when your code may branch and the property might be a different one in different cases. However, for something as simple as what you have, it is generally best to just use the property itself:


object.style.left = '20px';

However, this (what you posted) is perfectly fine:


var prop='style';

var direction='left';

object[prop][direction]="20px";

But there aren't any other values for prop that would work in such a case, at least not any that I can think of.

I have used this sort of approach in my Omni Slide Menu:

http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm

and in my Image Fader script:

http://home.comcast.net/~jscheuer1/side/files/fade_advanced_preset_write.htm

dukevn
07-16-2008, 01:59 PM
It can come in handy in certain situations to define and bracket properties, particularly when your code may branch and the property might be a different one in different cases. However, for something as simple as what you have, it is generally best to just use the property itself:


object.style.left = '20px';

However, this (what you posted) is perfectly fine:


var prop='style';

var direction='left';

object[prop][direction]="20px";

But there aren't any other values for prop that would work in such a case, at least not any that I can think of.

I have used this sort of approach in my Omni Slide Menu:

http://www.dynamicdrive.com/dynamicindex1/omnislide/index.htm

and in my Image Fader script:

http://home.comcast.net/~jscheuer1/side/files/fade_advanced_preset_write.htm

Thanks John. By the way, I like your Image Fader very much. I never thought that images can be faded :).

D.

jscheuer1
07-16-2008, 02:56 PM
I didn't think that image fading was all that new. What's interesting about the image fading script though, in relation to this thread, is doing this sort of thing in the code:


op_obj[fade.prprt]=op;

Which demonstrates code branching via predefined terms which vary by the objects available.

For instance, in IE 6 +, the op_obj will be an image object's filters object's 0 property, fade.prprt will be 'opacity', and op will be a number from 0 to 100.

In FF, op_obj will be an image object's style property, fade.prprt will be 'opacity', and op will be a number from 0 to .99.

In Safari 2, op_obj will be an image object's style property, fade.prprt will be 'KhtmlOpacity', and op will be a number from 0 to .99.

None of this is determined by sniffing the browser identity though, but by what objects are available when the code runs.

dukevn
07-17-2008, 05:39 AM
I didn't think that image fading was all that new. What's interesting about the image fading script though, in relation to this thread, is doing this sort of thing in the code:


op_obj[fade.prprt]=op;

Which demonstrates code branching via predefined terms which vary by the objects available.

For instance, in IE 6 +, the op_obj will be an image object's filters object's 0 property, fade.prprt will be 'opacity', and op will be a number from 0 to 100.

In FF, op_obj will be an image object's style property, fade.prprt will be 'opacity', and op will be a number from 0 to .99.

In Safari 2, op_obj will be an image object's style property, fade.prprt will be 'KhtmlOpacity', and op will be a number from 0 to .99.

None of this is determined by sniffing the browser identity though, but by what objects are available when the code runs.

Thanks John for your explanation. While I still do not understand your codes, I like it so much that I want to apply it to ProHTML Ticker. I created a thread here (http://www.dynamicdrive.com/forums/showthread.php?p=151619#post151619) in case you have any idea to help me out.

Thanks,

D.