Results 1 to 7 of 7

Thread: text variable in syntax

  1. #1
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default text variable in syntax

    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:
    Code:
    object.style.left="20px"
    I want to control the direction, so I change it to
    Code:
    var direction='left'
    ...
    object.style.direction="20px"
    Is it correct, or it is impossible to do that?

    Thanks,

    D.

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by dukevn View Post
    for the below code:
    Code:
    object.style.left="20px"
    I want to control the direction, so I change it to
    Code:
    var direction='left'
    ...
    object.style.direction="20px"
    Is it correct, or it is impossible to do that?

    Thanks,

    D.
    Try
    Code:
    var direction='left';
    
    object.style[direction]="20px"

  3. #3
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by clueful View Post
    Try
    Code:
    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
    Code:
    var prop='style';
    
    object[prop].left="20px"
    and if I want to control both properties
    Code:
    var prop='style';
    
    var direction='left'
    
    object[prop][direction]="20px"
    Is it correct?

    Thanks,

    D.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    object.style.left = '20px';
    However, this (what you posted) is perfectly fine:

    Code:
    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/dynamici...lide/index.htm

    and in my Image Fader script:

    http://home.comcast.net/~jscheuer1/s...eset_write.htm
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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:

    Code:
    object.style.left = '20px';
    However, this (what you posted) is perfectly fine:

    Code:
    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/dynamici...lide/index.htm

    and in my Image Fader script:

    http://home.comcast.net/~jscheuer1/s...eset_write.htm
    Thanks John. By the way, I like your Image Fader very much. I never thought that images can be faded .

    D.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2008
    Posts
    34
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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:

    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/s...619#post151619) in case you have any idea to help me out.

    Thanks,

    D.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •