Forum Moderators: open
I have a web portal I have developed where I have used frames for the navigation system. There is a header frame, left hand "menu" frame, a "main" frame and a "footer" frame. Everything link that a user clicks is displayed in the "main" frame.
I want to give my users a function called "New Window" in the "footer" frame that will "break" the main frame and open whatever is there in a separate
window. This will allow me to add more interesting contents since I will no longer be "trapping" users and sites in my frame system. The site where I would like to use this function is at www.caltrade.com.
I posted this issue on the usenet a while back and received this response:
"To open the document that is in the main frame in the same window... In the bottom frame...
<a href="#" onClick="top.location = top.frames
['mainFrameName'].document.location;return false;">click here to open
the document in this window</a>
where mainFrameName is the name you gave the main frame in the frameset.
If you want it in a new window...
<a href="#" onClick="window.open(top.frames
['mainFrameName'].document.location,'','');return false;">click here to
open the document in a new window</a>"
This ALMOST worked. In fact, it worked perfectly for pages that I created that were on my own server. It didn't work at all, however, for links from outside sites. When I tried it for outside links a copy of the "footer" frame would open in the "main" frame. I learned later that this is because some kind of Javascript security protocol was violated.
I am not a programmer, and unfortunately don't have money right now to hire one, but this function is really important for my site if there is some way to make it work. It would allow me to use the frames for navigation- which I like, but still allow the user to visit the links independently- and this would also be less offensive to the managers of other sites since I wouldn't be trapping their work in my frames.
Could someone tell me if this is absolutely, positively impossible? Is some kind of workaround possible? For example, would it be possible to somehow store the url that is displayed in the main frame and then open it in a new window? Any other ideas on how to make this work (even non-Javascript) would be appreciated.
Thanks
Rob
The Javascript security violation occurred when you tried to get the URL of the remote site and use it in your script.
It sounds like you want the visitor to choose IF they want to break out of the frameset. That will require some kind of programmatic solution. (We use PHP for this.)
If you want EVERY link in YOUR document in the main frame to open in a new window or to take over the frameset entirely, then you can simply add this to the HEAD of your document from which the visitor will click to go to a remote URL:
<base target="_top"> or <base target="_blank"> or "_new"
It's a little complex to get into the PHP implementation in this Javascript thread, however you should be able to find a copy-and-paste version in the PHP thread. (Of course, your server needs to have PHP installed and enabled for your use.)
<a href="#" onClick="top.location = top.frames
['mainFrameName'].document.location;return false;">click here to open
the document in this window</a>
could very easily be expressed as:
frameHref=self.location.href;
document.write('<a href="'+frameHref+'" target="_self">click here to open the document in this frame (normal)</a>');
document.write('<a href="'+frameHref+'" target="_top">click here to open the document in this window</a>');
corollaries:
document.write('<a href="'+frameHref+'" target="_blank">click here to open the document in a blank window</a>');
and:
document.write('<a href="'+frameHref+'" target="_new">click here to open the document in a new window</a>');
or even without Javascript as:
<a href="http://www.yahoo.com" target="_top">click here to open the document in this window</a>
if you have that kind of control over the link code.
You may be able to use a variable in a frame that you modify with your own, controllable* code to set the value of the 'frameHref' variable.
*I mean that you can't go: "frameHref's value is whatever URI is in that frame".
You have to go: "frameHref's value is 'http://www.yaddayadda.com'".
mainFrame (remote URIs)
MyFrame1 (your local URIs):
<script ... >
remotePage="";
</script>
<a href="http://www.yahoo.com" target="mainFrame" onclick="remotePage=this.href;top.document.frames['MyFrame2'].reload()">Yahoo!</a>
MyFrame2 (your 'open in new' links):
<script ... >
document.write('<a href="'+top.document.frames["MyFrame1"].remotePage+'" target="_top">Open In This Window</a>');
</script>