...

View Full Version : Where's my root window?



Darren
09-04-2007, 08:20 PM
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
<html><body>
<button onClick="loadPage()">Click me</button>
</body></html>
page2.htm
<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?

2reikis
09-04-2007, 08:46 PM
There are a several ways you can tell if your page is in a frame.


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

or


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

or perhaps


if (top.location != document.location.href) {
// You're in a frame!
}

to fix a page loading into a frame, you can use


top.location = document.location.href

in place of


// You're in a frame!

for a complete study, google "breaking out of frames".

HTH
2reikis

Darren
09-04-2007, 09:33 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum