0

I am using bootstrap modal to showcase my special offers on my site. But this modal pops up, every time the page gets refreshed. How do I restrict this modal to appear only once?

<!-- Modal start-->
<div class="container">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">/* header part */
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">/* Body part */
Some text
</div>
        </div>
        <div class="modal-footer">/* Footer part */

            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>  
</div>` This is the code used for the modal. What change do i need to make to make the modal pop up just once , rather than appearing on every page refresh??`
</div>  
<script>
$(document).ready(function(){
    $("#myModal").modal();
});
</script>
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69

3 Answers3

2

Just remove this:

<script>
$(document).ready(function(){
    $("#myModal").modal();
});
</script>

How you can toggle (hide/show) the modal:

$('#myModal').modal('show')
$('#myModal').modal('toggle')
$('#myModal').modal('hide')

Docs:

http://getbootstrap.com/javascript/#modals

If you want that the Modal only once appear i suggest jquery-cookies:

https://plugins.jquery.com/cookie/

Really simple to use:

<script>
$(document).ready(function(){

    if( !$.cookie('customer.offers.seen') {
       $("#myModal").modal();
       $.cookie('customer.offers.seen', true);
    }

});
</script>
pleinx
  • 616
  • 3
  • 8
1

You could use the HTML5 sessionStorage which stores the data for only one session. Basically it lives with the browser tab. If the user refresh it still there but if they close the tab its removed.

<script>
    $(document).ready(function(){
        if (sessionStorage.getItem("loaded") === null) {
          sessionStorage.setItem("loaded", "loaded");
          $("#myModal").modal();
        }
    });
</script>
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
0

By using cookie try the following

<script>
$(document).ready(function(){
    $("#myModal").modal();
});
</script>

into

<script>
$(document).ready(function(){
    var pop_up_show = $.cookie("pop_up_show"); 
    if(typeof pop_up_show != "undefined" && pop_up_show == "1"){

    }else{
        $.cookie("pop_up_show", "1");
        $("#myModal").modal();      
    }

});
</script>
Maths RkBala
  • 2,207
  • 3
  • 18
  • 21