简体   繁体   English

刷新后是否可以保留iframe网址?

[英]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" . 我想保留当前页面,但是每次刷新iframe时,都要回到默认页面url src="app.php" For example I'm on pr.php, and I accidentally refresh the page, I want to stay with that page. 例如,我在pr.php上,而我不小心刷新了页面,我想继续浏览该页面。 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. 您可以使用html5 localStorage保存iframe的当前网址,然后刷新页面尝试加载它,如果未定义则尝试加载初始页面。 Your iframe must not have src attribute at its tag declaration, you will set it with jquery. 您的iframe的标签声明中不得包含src属性,您将使用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. 当您的iframe加载页面时,会触发iframe加载事件。 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. 因此,在iframe中,如果您转到另一个链接,则会触发加载事件,然后将您现在的网址保存在浏览器中。

But please try to avoid iframes at any cost. 但请尝试不惜一切代价避免使用iframe。 And watch out how you operate the local storage... 并注意如何操作本地存储...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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