Link to home
Start Free TrialLog in
Avatar of prob
prob

asked on

Navigate page to iframe url??

We have a navigation system with tabs at the top which set the url of a large iframe underneath.

Within the iframes there are external links which open within the same iframe.

To 'break' out of the iframe, i navigate the page to the url of the iframe, ie

function SetPage()
{
window.location.href = window.parent.document.all.myFrame.src
}

Unfortunately, this function navigates to the default value of the iframe, not the current value.

How can I navigate to the current value of the iframe???

Here is an eg showing the problem.

Default.html follows, the iframe initally points to test.html

<html>
<body>
<p>
<input type="button" value="Emily"

onclick="frames['IFrameName'].location.href='http://www.cryer.co.uk/emily'">

Javascript:
<code>document.all.</code><code><em>IFrameName</em></code><code>.src='http://www.cryer.co.uk/emily'</code>

<p>

<input type="button"  value="Microsoft"
onclick="frames['IFrameName'].location.href='http://www.microsoft.com'">

Javascript:
<code>document.all.</code><code><em>IFrameName</em></code><code>.src='http://www.microsoft.com'</code>

<p>

<input type="button" value="Open Iframe wide" onclick="window.location.href =
window.parent.document.all.IFrameName.src">

Javascript: <code>window.location.href = window.parent.document.all.IFrameName.src</code>

<p>

<iframe SRC="test.html" name="IFrameName" width="100%" height="100" scrolling="no">

</body>
</html>


 
Avatar of spo0ky
spo0ky

use this:

<input type="button" value="Open Iframe wide" onclick="window.location.href = window.IFrameName.location.href">

but it will work only if documents located in the iframe are your own (on the same domain), if not access to the location of those documents will be denited
location.href=top.frames['IFramename'].location.href

Cd&
Avatar of prob

ASKER

Hmmm, neither suggestion works so far. I note the comment about the security restriction but surely you can get the url or scr of a document in an iframe?

Can we set the default scr of the iframe to its current value and then use window.parent.document.all.IFrameName.src  ??
> but surely you can get the url or scr of a document in an iframe?

NO you SURELY can't if the IFRAME and the script come from different domains - that's the point.
An IFRAME from the domain "microsoft.com" can't *read* from its parent in the domain "cryer.co.uk" and vice versa.
This is also true if one has the http: and the other https: protocol.

For both originating in the same domains the code from spoOky or Cd& would do.

CirTap
Why can't you simply set a var, where you'll be always able to find the current address in the iframe. Like this:

<html>
<head>
<script language="javascript">
var i = "test.html";     // default src in the iframe
</script>
</head>
<body>
<p>
<input type="button" value="Emily" onclick="frames['IFrameName'].location.href='http://www.cryer.co.uk/emily'; i='http://www.cryer.co.uk/emily'">
<p>
<input type="button"  value="Microsoft" onclick="frames['IFrameName'].location.href='http://www.microsoft.com'; i='http://www.microsoft.com'">
<p>
<input type="button" value="Open Iframe wide" onclick="window.location.href=i">
<p>
<iframe SRC="test.html" name="IFrameName" width="100%" height="100" scrolling="no">
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Member_2_547613
Member_2_547613
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prob

ASKER

I note cirtap's comments and will abandon this approach. Obviously what is happening now is that the default scr is being picked up by my function.

Does the same restriction apply to frames?
thanx for accepting these 'bad news' :)

> Does the same restriction apply to frames?
yepp. frame, iframe, popup-window, whatever contains an URI

you may only track the "new" URL once: when you change it and the i/frame goes off your domain (like your idea with the helper variable), but whatever happend inside this i/frame after that is inaccessible to your scripts.

although I've not tested this one: even an image comming from another domain should be inaccessible (eg. reading its .src, .width, .height)

All I could think of making other domain's content become "visual" part of your site is using a server side script on your domain that accepts a URL, gets that page (simulating a browser request) and forwards this content to your i/frame, NOW this page-copy originates from your own site, your server-script and your javascripts may access it (there are of coure other drawbacks here).
Note that this may violate copyrights etc. of the page owner.

Have fun
CirTap