Set SRC on IFRAME with dynamic name

Discussion in 'JavaScript' started by ruby, Jun 20, 2008.

  1. #1
    I have this:

    var randomnumber=Math.floor(Math.random()*10000);
    document.write('<iframe name="'+randomnumber+'"></iframe>');
    var a = 'frames[\''+randomnumber+'\'].location.href=loc';
    eval(a);

    When I execute it I get:

    Error: frames['1515'] has no properties

    Where '1515' is obviously the random number I am generating.

    Whats the issue? It should work... it works if I change that to a fixed value like:

    document.write('<iframe name="test"></iframe>');
    frames['test'].location.href=loc;


    Anyone know why this is happening?
     
    ruby, Jun 20, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    I have doubt that numbers are the problem which cannot be name or id of the object.

    make following change:

    Which will result in object name to be valid.

    regards
     
    Vooler, Jun 21, 2008 IP
  3. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #3
    I did exactly that and it worked a charm!
     
    ruby, Jun 21, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Glad it worked.

    Thanks for notifying.

    regards
     
    Vooler, Jun 22, 2008 IP
    ruby likes this.
  5. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #5
    Yer I did that as soon as I posted to see if it would work and it did.
     
    ruby, Jun 22, 2008 IP