最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display the hidden div after page reload? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论