1

I have implemented a modal popup in sharepoint 2016 from a tutorial on another site with this code:

    <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
<script language="javascript" type="text/javascript">  
    $(document).ready(function() {  
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', showModalPopUp);  
    });  

    function showModalPopUp() {  
        //Set options for Modal PopUp  
        var options = {  
            url: 'https://testurl.nl', //Set the url of the page  
            title: 'Testpopup', //Set the title for the pop up  
            allowMaximize: false,  
            showClose: true,  
            width: 600,  
            height: 400  
        };  
        //Invoke the modal dialog by passing in the options array variable  
        SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);  
        return false;  
    }  
</script>  

Now i need this popup to open once per user per day, or if that can't be done once per session. How do i do this using the code above?

1
2

The easiest way to achieve this is with a cookie which only lasts 24 hours.

What you'd do is check if there was a cookie and show the dialog if there isn't

if($.cookie('MyCookie') != 'DialogShown'){
   SP.SOD.executeFunc('sp.js', 'SP.ClientContext', showModalPopUp);  
  setCookie();
} 

Note: Above code write under document.ready method.

Here's the code to set cookies. Declare function in your file.

function setCookie(){
    $.cookie('MyCookie', 'DialogShown',
      {
         expires: date.getTime() + (24 * 60 * 60 * 1000) // now add 24 hours
      });
}

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.