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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iframe refresh child page only

Status
Not open for further replies.

PaleSeptember

Technical User
Feb 13, 2008
3
0
0
GB
I am creating a website with an iframe.
The parent page has layers that are visible or hidden depending on user action.

If a visiter refreshes the page, then the parent page reverts to it's original state which is not always appropriate therefore I would like the child page only to refresh when the visitor refreshes the page.

I am using a javascript code to ensure the page refreshes within the parent page but I don't know which bit of the code (if any) I can alter to stop the parent page refreshing at the same time.

Code:
<script type="text/javascript">
pageLoc = self.location;
pageAdd = top.location;
 
if (pageLoc == pageAdd) {
    contentSrc = escape(pageLoc);
    contPage = 'index.html?' ;
    top.location.href = contPage;
}
</script>

If this is not possible, could you tell me which bit of the code to change so that on refresh the parent page reverts to its original iframe source, not the current one.
Thank you for your help.
BTW idiot's guide needed here as you can probably tell by my wording.
 
PaleSeptember said:
I would like the child page only to refresh when the visitor refreshes the page.

No can do I'm afraid. The user is in control of their browser, if they wish to refresh the content of the main window, there isn't anything - javascript or otherwise - you can do to stop them. Perhaps a better solution to whatever it is you're looking to do might be to investigate HTTP sessions on the server side and maintain state that way.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Thank you for such a fast response.
And it isn't possible for the parent page to open with the original specified iframe source in it rather than the current one??
 
And it isn't possible for the parent page to open with the original specified iframe source in it rather than the current one??

There are two ways to achieve this - one would be server side, storing information as session variables and dynamically building your frameset that way.

The other would be to pass some information to the client through the URL and use javascript to read the information out and set the location of the IFRAME.

So you might pass the url:
[tt][/tt]

You can then read everything after the [tt]?[/tt] from the [tt]location.search[/tt] variable to get the url to point your frame at.

I prefer the server-side approach, because it will work regardless of whether or not a user has JS switched on or off. But sometimes we don't have much choice in these matters.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Thank you dwarfthrower for your help. Sorry to take so long to respond but I was trying to get it to work so I didn't have to bother you again but I've had to give in and ask for help again :(.
I am using the code from this website
And have altered it with your suggestion above to this:

<script type="text/javascript">
pageLoc = self.location;
pageAdd = top.location;

if (pageLoc == pageAdd) {
contentSrc = escape(pageLoc);
contPage = ' top.location.href = contPage;
}
</script>

But I still get the current iframe appearing as the source iframe rather than the specified page when the current page is refreshed.
I admit to knowing nothing about javascript. I can only cut and past the code in and follow instructions to change it so should I have altered something else too??
 
Hello,

I recommend do it via a cookie.

Ok very child page (iframe page) should create the cookie by having the following script in the <head> tag.

<script>
function createCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}
createCookie("usersAction","CHANGETHISNAME.html");
<script>

In the parent frame read the cookie, so put the following code into the <head> of the parent page.

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
</script>

The most important thing is this, you must put the following script under your iframe in the parent page. And the IDs must match!

<iframe src="" id="somename"></iframe>)

<script>
document.getElementById('somename').src = readCookie("usersAction");
</script>





Here the code working for a single HTML page. If you go over the code you can see at the iframe source is BBC but if you run the code GOOGLE appears instead.

<script>
function createCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}
createCookie("changesite","function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

document.getElementById('myiframe').src = readCookie("changesite");
</script>

<iframe src=" id="myiframe"></iframe>

<script>
document.getElementById('myiframe').src = readCookie("changesite");
</script>


Hope this helps!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top