SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    0 Post(s)
    0 Thread(s)

    iframe accessing variables


    is it possible to access javascript variables in an iframe src page from the parent page, or vice versa. How do you do this? What about the DOM?


  2. #2
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Melbourne Australia
    1 Post(s)
    0 Thread(s)
    If they both come from the same origin (ie the same domain), then each can access the other's variables as normal.

    To access the parent's scope from the child you can use window.parent and to access the child's scope from the parent you can use a couple of methods, such as window.frames.

    So, for instance, if you define a var myvar = 2 in the parent scope, you can access that in the iframe as window.parent.myvar.

    If the page inside the iframe comes from a different domain to the parent page then it cannot access anything from the parent (with only a very small number of exceptions, such as being able to reload/redirect the page). This same is mostly true of the parent page accessing anything from the child frame as well.
    [mmj] My magic jigsaw
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff


Posting Permissions

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