简体   繁体   中英

If it possible to retain iframe url after refresh?

I want to stay with my current page, but everytime I refresh the iframe go back to it's default page url src="app.php" . For example I'm on pr.php, and I accidentally refresh the page, I want to stay with that page. If it possible?

<a href="app.php" target="contents">APP</a><br/>
<a href="rfq.php" target="contents">RFQ</a><br/>
<a href="pr.php" target="contents">PR</a><br/>
<a href="addapp.php" target="name" onclick="modalWin(); return false;">Add APP</a><br/>
<a href="livesearch/index.php" target="contents">SEARCH</a><br/>

    <iframe name="contents" src="app.php" frameborder="0"  id="iframe" 
    frameborder="0" style="moz-overflow:hidden;moz-overflow-x:hidden;moz-overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"> </iframe>

You can use the html5 localStorage to save your iframe's current url, and you refresh the page try to load it, if it is undefined try to load the initial page. Your iframe must not have src attribute at its tag declaration, you will set it with jquery. I've done an approximation of what youu may want, I hope it works

$(document).ready(function() {
    currentLocation = localStorage.prevUrl || 'yourInitialSrc';

    $('#iframe').attr('src', currentLocation);
    $('#iframe').load(function() {
        localStorage.prevUrl = $(this)[0].contentWindow.location.href;
    })
})

iframe load event is triggered when your iframe loads a page. So in your iframe if you go to another link the load event is triggered and you save locally in your browser the url you are now.

But please try to avoid iframes at any cost. And watch out how you operate the local storage...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM