1

How can i create a popup that automatically shows when the user visit the homepage? The user also want an option to not show the popup anymore.

Here is my code:

ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");  
(function(){

 function initialize() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_current();

        OpenPopUpPageWithTitle(url, callback, width, height, title)
 }

     window['initialize'] = initialize;
  })();

_spBodyOnLoadFunctionNames.push("initialize");

My new code are:

On the startpage in a script editor:

  <script type="text/javascript">
  SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'sp.ui.dialog.js');

  function initialize() {

  var context = new SP.ClientContext.get_current();
  var web = context.get_current();
  var currentUser = web.get_userprofile.get_current();

  var options = {
  autoSize: true,
  title: "My Custom dialog",
  showClose: true,
  allowMaximize: true
  };

  SP.UI.ModalDialog.commonModalDialogOpen("/_layouts/test.aspx",options,closecallback, null); 

On the aspx page that popup shows:

<html>

function mDown(){

ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");  
function runCode(){
var currentUser;

var context = new SP.ClientContext.get_current();
var web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccessMethod, onRequestFail);

function onSuccessMethod(sender, args) {
var account = currentUser.get_loginName();
var currentUserAccount = account.substring(account.indexOf("|") + 1);
// alert(currentUserAccount);
createCookie(account,"showPopUp",10);
readCookie(account);

//obj.style.backgroundColor="#1ec5e5";
//obj.innerHTML= currentUserAccount;
}

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
 }

 function readCookie(name) {

var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
alert(nameEQ);
return null;
  }

  function onRequestFail(sender, args) {
  alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
 }
 }
 };
 </script>
 <body>
 <button id="myButton" onClick="mDown()">Click me!</button>
 </body>
 </html>

How can i check if the user has the cookie on the startpage?

  • Can you provide the definition of function OpenPopUpPageWithTitle – Arsalan Adam Khatri Apr 3 '14 at 9:39
  • What are we using the web for? – Robert Lindgren Apr 3 '14 at 10:05
2

Inside the custom page that you're opening in a popup, add a checkbox or button (Don't show this again), then bind click handler that creates a cookie (username, showPopup)

Inside your function,

function initialize() {
  var context = new SP.ClientContext.get_current();
  var web = context.get_current();

  // 1. get current username 
  // 2. check if the cookie exist for current username
  // 3. You can use User Profile to store the property instead of cookie
  if(showPopup == true)
     return;
  else
     OpenPopUpPageWithTitle(url, callback, width, height, title)
}

$(document).ready(function() {
   $('#myButton').on('click', function () {
     //Call your function
     initialize();
   });
});

As suggested by Arsalan, use SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'sp.ui.dialog.js');

  • Thank you i will try to seek for answear how to work with user profiles, if you dont have any suggestions. Thank you – Anders Apr 4 '14 at 9:21
  • I would suggest use cookies instead of User Profiles. – Aveenav Apr 4 '14 at 14:34
  • ok, how can i fire sharepoint jsom code in an event handler (on click) ? – Anders Apr 4 '14 at 14:36
  • See the updated answer. – Aveenav Apr 4 '14 at 14:46
  • See my updated answear – Anders Apr 4 '14 at 15:32
1

Use as below:

SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'sp.ui.dialog.js');

function initialize() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_current();

    OpenPopUpPageWithTitle(url, callback, width, height, title)
}

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.