Tim Tim - 7 months ago 11
Javascript Question

Run Javascript once on homepage and remember

I have a Bootstrap modal that shows on page load. However, I want it to not show again when the user visits this page again. It can load again if the browser is closed(ends the browsing session on the website). As of right now, modal will load every time I'm in or revisit index page..


index.html


<script type="text/javascript">
$(window).load(function(){
$('.bs-example-modal-sm').modal('show');
});
</script>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog modal-sm">
<div class="modal-content">
...
</div>
</div>
</div>

Answer

You can use sessionStorage.

Write your script as

<script type="text/javascript"> 
    $(window).load(function(){ 
        if(!sessionStorage.visited) 
        {
           sessionStorage.visited = true;
           $('.bs-example-modal-sm').modal('show');
        }
    });
</script>
Comments