Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve URL from a frame?

Status
Not open for further replies.

sorenchr

Programmer
May 11, 2008
7
0
0
DK
Hi,

I have a fairly simple frameset:

Code:

<frameset rows="50%,50%"> <frame src="login/login.php" name="main"> <frame src="blank.htm" name="redirector" noresize scrolling="no"> </frameset>


The purpose of my site is to let the user click a link in the 'main' frame, which then loads in the 'redirector' frame, which works out fine. However, I was wondering if I was able to retrieve that particular URL in the 'redirector' window, and then display it in the 'main' window. For example, the main window would read the following after clicking a link called google.

"You chose to visist
Best regards
Soren
 
In your case, with 2 frames, you ought to be able to get at the properties of each frame considered as a window by using the construct top.frames[1].document.URL.

This depends on the "redirector" frame being 2nd in your HTML, but should give the right result. You might need some JavaScript attached to the onLoad event in the redirector to update the link in the "main" frame when things change in the redirector window. An easy way to provide this functionality is to define an update function as part of the "main" frame, and then the reference to it from within the "redirector" frame would be top.frames[0].update().

 
Personally, to avoid any cross-domain scripting issues, I'd do it upon clicking the link, and reading its HREF attribute.

You should be able to assign an onclick to all links in your frame by looping around either of these:

Code:
document.links

document.getElementsByTagName('a')

Then simply assign an onclick handler to each link in turn which does whatever you want it to do with "this.href".

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
The purpose of my site is to let the user click a link in the 'main' frame, which then loads in the 'redirector' frame

Although I have to ask - what is the point of that? Why not just let the user visit the site they want by typing in the URL and they get more screen real estate due to not having your frameset in place?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I know the idea seems fairly ridiculous, however I can assure you it makes sense in a bigger context =).

As for the answers so far, they have been very helpful, I wasn't exactly sure if javascript had a function where you could retrieve a URL from a frame.

However i have encountered yet another problem. Lets assume that the user visits google, and the message still appears upon clicking the link. However, suppose that google redirects itself to yahoo.com upon it being loaded. Now the process is the following:

Click on link -> Message appears -> Google.com is loaded -> Redirects to yahoo.com

I was wondering if i could somehow time this message or let javascript determine the final URL in the window. So that the process occurs like this:

Click on link -> Google.com is loaded -> Redirects to yahoo.com -> Message appears

 
So that the user gets the message, "You chose yahoo.com!" instead of google.
 
As I noted above, if you wait for the onload event to fire in the target window, the URL there will reflect the final source of the HTML. You will need some JavaScript in this onload event handler to pass that info back to your "main" window.

Oh, BTW, event handlers are noticeably different under IE than under EOMB (every other modern browser). Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top