Avatar of Oliver2000
Oliver2000Flag for Brazil

asked on 

History back for iFrame from Parent Window

Hi experts,

I do have a page with 6 different iframes and would like to control from the parent window the history back function of each iframe separated. Lets say I have 2 iframes with the same URL inside, in one of the iframes the user clicks deeper for a couple of levels. And now I have inside my original parent a BACK button which ONLY influence the iframe the user want to go back but not for the others or even worse for the parent.

I made an example code to explain better

<html>
	<body>
This is a simple test
<br><br>
<a href="#" onClick="document.getElementById('1').contentWindow.history.back(-1);">FRAME 1 BACK</a>&nbsp;&nbsp;&nbsp;<a href="#" onClick="document.getElementById('2').contentWindow.history.back(-1);">FRAME 2 BACK</a>
<br><br>
iFrame 1<br><iframe id="1" name="1" src="http://www.bing.com" style="width: 90%; height: 300px"></iframe><br><br>
iFrame 2<br><iframe id="2" name="2" src="http://www.bing.com" style="width: 90%; height: 300px"></iframe><br><br>
</body>
</html>

Open in new window


Of course this is just a fast test code and not correct HTML, I tried just to show the problem for me.
Right now I can click on any of the history back buttons without anything happens.

Anybody with an idea? (can be also jquery)

Thank you
HTMLJavaScriptjQuery

Avatar of undefined
Last Comment
Oliver2000
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

you probably will hit an Access Denied error since the URLs in your iframes come with a different domain name.

User generated image
you probably need to test some URLs with same domain instead
Avatar of Oliver2000
Oliver2000
Flag of Brazil image

ASKER

Hi Ryan,

Thank you for your reply but this is not the issue. There is no permission problem involved. History-Back does not influence the actual contants of the external domain but only influence the HREF of the iframe which is controlled by parent page.

Check out this test link: http://www.internet-arts.com/frame_test/ All pages are loaded from same domain, this does not change anything and still the history back for the iframes is not working.

How ever as you pointed out in your post my demo html code was miss-leading because it throughs an permission error but as you can see with same domain does not work either.
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

You are using

frame1.contentWindow.history.go(-1)

Open in new window


but "frame1" is only a valid variable in internet explorer (in IE elements with id are created as variables in window Object, i don't know if this still works in newer versions ) .
To ensure compatibility you need to change it to a Element reference:

document.getElementById('frame1').contentWindow.history.go(-1)

Open in new window


I've changed and tested (Also for frame2 link) and it's working for me.
Avatar of Oliver2000
Oliver2000
Flag of Brazil image

ASKER

Result is the same with document.getElementById('frame1').contentWindow.history.go(-1)
I updated the test URL http://www.internet-arts.com/frame_test/ in any browser. As you can see in the initial post I tried this already before.
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

The links are  working doing "back", but it seems the "history" object is shared between all iframes,
any change on iframe's url are added to a global history in that order

So, calling window.history.go(-1) and calling document.getElementById('frame1').contentWindow.history.go(-1) are equivalent
Also, between different browsers its behavior may be different

Finding out some advices with the topic "multiple iframe history", i've found:
http://khaidoan.wikidot.com/iframe-and-browser-history
http://www.webdeveloper.com/forum/showthread.php?232547-Multiple-Iframes-Back-problem
http://stackoverflow.com/questions/20230226/multiple-iframes-with-individual-back-buttons


Probably, you cannot control the history of individual iframes, unless you are using a different approach (cookies, session data)
Avatar of Francisco Igor
Francisco Igor
Flag of Canada image

See if the new history management in HTML5 can help you controlling the browser history:
https://developer.mozilla.org/en-US/docs/Web/API/History_API

And for backward compatibility you can use the history.js from the Ext-js library
https://github.com/Jackslocum/extjs-history
Avatar of Oliver2000
Oliver2000
Flag of Brazil image

ASKER

All options not working really so I thought about another solution and actually keep an array with all URL changes of the iframe and the back button actually changes the SRC of the iframe back to the URL from the own history for this iframe.

How ever now I have another problem. I made a function I call each time the iframe has loaded (onLoad) but I have a problem with the dynamic variable name in this case.

function iframe_update_history(frame){
	
current_iframe_url = document.getElementById(frame).contentWindow.location.href;
  window['current_iframe_active'] = ("history_" + frame);
   window['current_iframe_active'].push(current_iframe_url);         
}

Open in new window


I call this for example iframe_update_history('frame_one');

The function takes now the current HREF from the frame "frame_one" and I tried to push this into the array called history_iframe_one. How ever I get an error push is not a function because the push function seem to not get the name of my array as I want.

How can I use array push with a dynamic variable in front?
ASKER CERTIFIED SOLUTION
Avatar of Oliver2000
Oliver2000
Flag of Brazil image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Oliver2000
Oliver2000
Flag of Brazil image

ASKER

It turns out there is not really good way to do this. If you have several iframes in one page the history back function is a mess in any browser and all the usual functions like history.back() etc. dont work well. Also if you hit the last position of the iframe history the entire parent reloads. I did not find any working solution and that for created my own.

1. On each iframe I use onLOAD to call a function and write the new loaded URL into an array.
2. On my back button I check which iframe is currently active (last touched) and just change the SRC to the last added URL from the array plus delete the last entry array.pop()
3. Once I hit the last entry I just jump and dont do anything.

Works perfect and is pretty simple to do.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo