简体   繁体   中英

How to display the hidden div after page reload?

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.com/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

You 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.com/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 comments 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.com/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> 

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