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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Where's my root window?

    Is there an elegant way to determine if my javascript is running at the window level or within a frame? here's two files to demonstrate what I'm talking about:

    page1.htm
    PHP Code:
    <html><body>
    <
    button onClick="loadPage()">Click me</button>
    </
    body></html
    page2.htm
    PHP Code:
    <html><body>
    <
    iframe src="page1.htm" />
    </
    body></html
    My function loadPage() is using window.document.open() and writing out a new page. But when it is ran inside a frame (such as it does in the second example above), it writes out the new page into the frame. I want it to overwrite the window at the root level. Both examples use the same function. I was using window.parent, based on the behavior I've observed that if I'm already at the root window, window.parent seems to return itself, but that seems like a quirk and I'm not sure I can rely on that.

    Thanks in advance,
    Darren

    edited:
    I just discovered that my problem appears to be tied to the doctype that I send in the document.write() command. When I remove the doctype, the frame button will reload at the root window. But when I have my doctype specified (<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>) it stops working when I hit the button from within a frame.
    Hmmm... any ideas?
    Last edited by Darren; 09-04-2007 at 08:46 PM.

  • #2
    Regular Coder 2reikis's Avatar
    Join Date
    Nov 2005
    Location
    New Mexico, USA
    Posts
    174
    Thanks
    18
    Thanked 13 Times in 13 Posts

    Take your pick

    There are a several ways you can tell if your page is in a frame.

    Code:
    if (window.location!=location) {
      //  You're in a frame!
    }
    or

    Code:
    if(parent.frames.length>=1) { 
      //  You're in a frame!
    }
    or perhaps

    Code:
    if (top.location != document.location.href) {
      // You're in a frame!
    }
    to fix a page loading into a frame, you can use

    Code:
    top.location = document.location.href
    in place of

    Code:
      // You're in a frame!
    for a complete study, google "breaking out of frames".

    HTH
    2reikis
    Last edited by 2reikis; 09-04-2007 at 08:49 PM. Reason: cross posted

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    59
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Here's the good and bad. First--the bad--I discovered my problem had something to do with my writing out the doctype instead of the actual window reference. So the whole 'break out of frames' (which I did google and research) didn't help. However--the good--your use of 'window.top' is cleaner than my use of 'window.parent'. I replaced all of those references and I feel better about that now.

    Back to the problem, however, I was doing the following:

    window.top.document.writeln(gDocType);
    window.top.document.writeln(strHtml);
    window.top.document.close();

    which wasn't working when I ran it from within a frame. The window would clear and then just hang.

    I changed it to the following:

    window.top.document.writeln(gDocType+"\n"+strHtml);
    window.top.document.close();

    and now it works. So I'm assuming there is a problem with using multiple writeln() calls when writing 'out' of a frame. It works when I'm not in a frame, but not when I am. Don't understand it... but it works... I guess that's what counts.

    Thanks for the tip on using 'window.top'.

    Darren


  •  

    Posting Permissions

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