admin管理员组文章数量:1426052
I have a requirement that when click on a button the page need to reload.after reload i need to show the hidden div.
below is my requirement which describe my problem?
1.In my html code consist of some text along with button and in this page only by default i am hiding some text div 2.when click on the button i am reloading the page .after reload the page i want show hidden div
<!DOCTYPE html>
<html>
<head>
<script src=".1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#test2").css("visibility","hidden");
alert("reloaded");
$("#p1").click(function(){
setTimeout(function(e){
alert("inside time out");
$("#p2").css("visibility","visible");
},3000);
location.reload();
});
});
</script>
</head>
<body>
<div id="myDiv">
<p id="p1">This is sample text</p>
</div>
<div id="test2">
<p id="p2">this is invisible text</p>
</div>
</body>
</html>
I have a requirement that when click on a button the page need to reload.after reload i need to show the hidden div.
below is my requirement which describe my problem?
1.In my html code consist of some text along with button and in this page only by default i am hiding some text div 2.when click on the button i am reloading the page .after reload the page i want show hidden div
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#test2").css("visibility","hidden");
alert("reloaded");
$("#p1").click(function(){
setTimeout(function(e){
alert("inside time out");
$("#p2").css("visibility","visible");
},3000);
location.reload();
});
});
</script>
</head>
<body>
<div id="myDiv">
<p id="p1">This is sample text</p>
</div>
<div id="test2">
<p id="p2">this is invisible text</p>
</div>
</body>
</html>
Thanks in advance
Share Improve this question edited Feb 9, 2017 at 17:16 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Feb 9, 2017 at 17:12 sureshsuresh 451 silver badge8 bronze badges 3- I'd start by adding a button to your code since your requirement is that the page reloads after the click of a button. – Scott Marcus Commented Feb 9, 2017 at 17:17
- you can use cookie, or hash, web storage – Laurianti Commented Feb 9, 2017 at 17:19
- Suresh, why haven't you accepted an answer to any of your questions? – Michael Coker Commented Jun 6, 2017 at 15:33
2 Answers
Reset to default 2You can set a localStorage
item when a user clicks the button, and on page load look for the localStorage
item and conditionally show the hidden div.
var $hidden = $('.hidden');
localStorage.getItem('show') && $hidden.show();
$('button').on('click',function() {
localStorage.setItem('show',true);
window.location.reload(false);
})
.hidden {
display: none;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hidden">hidden</div>
<button>click</button>
First, if your requirement is to have a button be clicked, you'll need a button, not a paragraph.
Next, instead of the visibility
property (which still allocates space on the page for the element even when it is not shown), use display
(which does not).
Most importantly, if you reload the document, then any local variables you have will be lost. You need to persist some kind of "flag" between page loads. This can be done in a variety of ways (cookies, sessionStorage, localStorage, server-side), but localStorage
is probably the simplest.
This code won't actually run, here in the Stack Overflow snippet environment due to sandboxing, but you can see a working version of it here.
See other ments inline:
$(document).ready(function(){
// Check to see if this is a page reload or not by seeing if a value was placed
// into localStorage from a previous page load
if(localStorage.getItem("loadedEarlier")){
// Page has already loaded earlier
$("#test2").css("display","block");
}
$("#btn").click(function(){
location.reload();
});
// Place a value into localStorage
localStorage.setItem("loadedEarlier", "yes")
});
/* No need for JavaScript to initially hide the element which
can cause the usre to see it momentarially before the JS runs.
Set its default display to none instead. */
#test2 { display:none; }
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn">Click to Reload</button>
<div id="test2"><p id="p2">this is invisible text</p></div>
本文标签: javascriptHow to display the hidden div after page reloadStack Overflow
版权声明:本文标题:javascript - How to display the hidden div after page reload? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745395207a2656782.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论