Reputation: 157
I have an iframe on my index page with a NAME 'mciframe' and ID 'mciframe'.
That iframe will serve as the primary 'content window' for everything viewed on my site.
When my site visitors hit their browser's 'refresh' button of the 'index' page, I would like whatever content they are currently viewing within the iframe at that particular time to refresh, instead of the iframe returning to it's 'default' source page.
I would have posted an example code here, but I did my due diligence and research already, and have already tried about 40 different codes I have gotten from this site today and none of them have worked so far.
Maybe those examples were out of date or not applicable to my particular issue... I don't know.
Hopefully someone have a working suggestion.
Thanks in advance for all suggestions.
Upvotes: 0
Views: 1105
Reputation: 192
You can use localstorage to save current iframe page!
Here are the steps i'd use:
1. When user leaves page save current iframe page into local storage.
2. When you load page check if there is saved page in your local storage.
3. If there is, set iframe.src
to that value.
example:
var iframe = document.getElementById("yourIframe");
window.onbeforeunload = saveCurrentPage;
iframe.onload = setPageToBeforeUnload;
function saveCurrentPage(){
localStorage.setItem("lastPageInIframe", iframe.src);
}
function setPageToBeforeUnload(){
iframe.src = localStorage.getItem("lastPageInIframe");
}
Please tell me if it worked for you!
Upvotes: 1
Reputation: 34
You can set iframe src attribute from server side and use onbeforeunload to send users last url just before refresh;
or you set 'src' from browser side, hold it in cookie and update whenever you want.
Upvotes: 0