[英]How to take back a page to its previous state by clicking the back button of the browser in javascript
I have a page with a link
to a form
. 我有一个页面,其中包含
link
form
的link
。 After clicking the link
the form
shows up and the link
disappears. 单击
link
后, link
显示该form
, link
消失。 The problem that i have is, when i click the browser's back button
, the values of the URL is changed, but the state of the page doesn't go back to previous state. 我遇到的问题是,当我单击浏览器的“
back button
,URL的值已更改,但是页面的状态不会返回到先前的状态。 The form
should disappear and the link
shows back. form
应该消失, link
显示回来。 Also on reload when the form
is visible, the page goes back to its first state, which i need to prevent from happening. 同样在
form
可见时重新加载时,页面将返回其第一状态,我需要防止这种情况发生。
Code : 代码:
<html>
<style>
.titimmo {
text-align: center;
padding: 10px;
font-size: 14pt;
background-color: #CC3300;
display: block;
}
.hidden {
display: none;
}
.visible {
display: block;
}
#formContainer {
padding: 1em 0 1em 2em;
background-color: #E8E8E8;
margin: 1em 0 1em 2em;
width: 88.9%;
}
#formContainer h4 {
color: #FF3300;
}
</style>
<body>
<div id="categContainer1">
<div class="titimmo"><a href="add.php?c=realestate" id="a_categ">Real Estate</a></div>
</div>
<div id="formContainer" class="hidden">
<form action="add.php" method="post">
<h4>Location :</h4>
<input type="text" name="made"/>
<h4>Price :</h4>
<input type="text" name="modele"/><br/><br/>
</form>
</div>
<script>
function stepone() {
document.getElementById('a_categ').onclick = function () {
document.getElementById('categContainer1').className += " hidden";
document.getElementById('formContainer').className = "visible";
window.history.pushState('Form', 'My form', this.getAttribute("href"));
return false
};
}
stepone();
</script>
</body>
</html>
There are two things you need to do to make it work: 要使其正常工作,需要做两件事:
This is a basic example: 这是一个基本示例:
var formVisible = localStorage.formVisible || false;
var cContainer = document.getElementById('categContainer1');
var fContainer = document.getElementById('formContainer');
function formOpen(e) {
cContainer.className = "hidden";
fContainer.className = "visible";
window.history.pushState('Form', 'My form', this.getAttribute("href"));
localStorage.formVisible = 'Y';
return false;
};
function formClose(e) {
cContainer.className = "visible";
fContainer.className = "hidden";
localStorage.removeItem( 'formVisible' );
};
if( formVisible ) formOpen();
document.getElementById('a_categ').onclick = formOpen;
window.onpopstate = formClose;
var formVisible = localStorage.formVisible || false; var cContainer = document.getElementById('categContainer1'); var fContainer = document.getElementById('formContainer'); function formOpen(e) { cContainer.className = "hidden"; fContainer.className = "visible"; window.history.pushState('Form', 'My form', this.getAttribute("href")); localStorage.formVisible = 'Y'; return false; }; function formClose(e) { cContainer.className = "visible"; fContainer.className = "hidden"; localStorage.removeItem( 'formVisible' ); }; if( formVisible ) formOpen(); document.getElementById('a_categ').onclick = formOpen; window.onpopstate = formClose;
.titimmo { text-align: center; padding: 10px; font-size: 14pt; background-color: #CC3300; display: block; } .hidden { display: none; } .visible { display: block; } #formContainer { padding: 1em 0 1em 2em; background-color: #E8E8E8; margin: 1em 0 1em 2em; width: 88.9%; } #formContainer h4 { color: #FF3300; }
<div id="categContainer1"> <div class="titimmo"> <a href="add.php?c=realestate" id="a_categ">Real Estate</a> </div> </div> <div id="formContainer" class="hidden"> <form action="add.php" method="post"> <h4>Location :</h4> <input type="text" name="made" /> <h4>Price :</h4> <input type="text" name="modele" /> </form> </div>
Also on Fiddle , where you can actually see how it works . 在Fiddle上 , 您实际上可以看到它是如何工作的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.