Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    Scotland
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using JavaScript Variables

    Hi,

    I am having problems using/setting a javascript variable.


    Consider the following function that I am using:

    function getLayer(id,framename) {
    if (framename == "top.index_main.main")
    return top.index_main.main.document.all[id];
    if (framename == "top.index_main.news_main")
    return top.index_main.news_main.document.all[id];
    if (framename == "top.index_nav")
    return top.index_nav.document.all[id];
    if (framename == "top.index_main")
    return top.index_main.document.all[id];
    }

    However, if I try to use the following, which would seem to me to be the obvious way to do it, I get errors:

    function getLayer(id,framename) {
    return framename.document.all[id];
    }

    My list of frame names is growing, and I do not want to have to use the first method. Is there something that I am doing wrong in my second method to explain why it wont work?

    Any suggestions apreciated!

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if (framename == "top.index_main.main") //framename is string
    return top.index_main.main.document.all[id]; //now it's not

    JS code like top.index_main.main is not a string; dot syntax consists of dots (d'oh ) and identifiers (tokens/variable names). Can't simply 'load a string' into one...you need to use eval() - ugh - or reference with associative array notation, which allows easy mixing of strings and dot syntax.

    function getLayer(id,framename) {
    return frames[framename].document.all[id];
    }

    http://www.forerunners.org/WebLibrar...pt/ch07_06.htm

    Shouldn't be using IE-only referencing, but that's another thread...

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    Scotland
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you cheesebagpipe.

    I tried something similar and it still didn't work:

    framename = "top.index_main";

    if (frames[framename].document.all[object] != null) {
    frames[framename].document.all[object].visibility='visiblity';
    }

    I receive the error " 'frames[...].document' is null or not an object ".

    Any ideas?

    PS. my function is cross browser, I have just put in the IE section to keep it simple.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    try:

    top.frames[framename].document.all[object]
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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