5

I used a simple Java Script code as a text file and linked from a webpart on the page to show a pop up window as an announcement. but It's gonna be annoying if it appears every time user go to the home page :( Is there any way I can handle this? I should mention that it's SP2010. My simple code is :

<script type="text/javascript">
    window.setTimeout(popupwin,5000);
    function popupwin(){
        ExecuteOrDelayUntilScriptLoaded(function ()
        {
            var options = SP.UI.$create_DialogOptions();
            options.title = "Announcements";
            options.width = 400;
            options.height = 400;
            options.url = "/SitePages/Announcements.aspx";
            SP.UI.ModalDialog.showModalDialog(options);
        },'sp.js');
    }
</script>
3

Announcement will open once for session using sessionStorage

<script type="text/javascript">
if (sessionStorage.getItem('AnnouncementOnce') !== 'true') {
window.setTimeout(popupwin,5000);
    function popupwin(){
        ExecuteOrDelayUntilScriptLoaded(function ()
        {
            var options = SP.UI.$create_DialogOptions();
            options.title = "Announcements";
            options.width = 400;
            options.height = 400;
            options.url = "/SitePages/Announcements.aspx";
            SP.UI.ModalDialog.showModalDialog(options);
        },'sp.js');
    }

sessionStorage.setItem('AnnouncementOnce','true');
}
</script>
  • The data stored in the session storage are lost when the browser session are closed. – Christoffer Jun 19 '17 at 10:39
  • This is exactly what I want and It works great !! Thanks :) – Farzaneh Aqabeyki Jun 20 '17 at 12:44
2

There are some ways to do this. You could just use cookies too see if the user has seen the popup for that day and if not show the popup and then set the cookie. I have used this method in the past. A similar approach might be local storage but I haven't tested that yet. Depending on the amount of users your targeting you could also just store the checks in a list. It just depends on the scope of what you want to accomplish.

  • Let's hope the announcement is NOT to comply with the EU directive requiring site owners to display a notification that their site uses cookies. – Pascal Ganaye Jun 19 '17 at 13:07
  • Thanks for the information, I'm really new to java script so looking for a very simple way. – Farzaneh Aqabeyki Jun 20 '17 at 12:45
1

As @spevilgenius mentioned, consider using cookies. This was the route we took when deciding to show a pop up on the home page of our intranet

// Add new cookie.
date = new Date();
dateUTCString = date.toUTCString(); 
$.cookie('||Wrapper-' + dateUTCString, 'HomePage', {path: '/', expires: 1});  // expire cookie after 1 day.
console.log('New cookie added.');
cookieCount ++;
console.log('Number of cookies: ' + cookieCount);
//Get cookies.
var cookies = document.cookie.split('||');
var cookieCount = cookies.length -1;
  • Hi,Thanks for the sample, I tried the cookie method actually but didnt work for me. so I decided to use session instead. – Farzaneh Aqabeyki Jun 20 '17 at 12:50

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.