I have created a visual webpart in SP2013. and created a page and add that WebPart to the page http://server Name/Pages/survey.aspx

to make this WebPart to show as a popup on the homepage every time page loads, I have added the below script (PupopSurvey.js) to Site Assets then added a Content Editor Webpart to the Home Page with the link to /SiteAssets/PupopSurvey.js.

 <script type="text/ecmascript">
var str = document.cookie.substring(0, 8);
 alert("Cookie= "+ str);
if ( (str == "Canceled!") || (str == "PostBack") ) { 

ExecuteOrDelayUntilScriptLoaded(ShowDialog, "sp.js");

function ShowDialog() {

        var options = {

            url: 'http://ServerName/Pages/survey.aspx',
            height: 900,



function CloseCallback(result, target) {

     if(result==SP.UI.DialogResult.OK) {



          SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel,'cancel Clicked');



When the home page loads, the survey.aspx page comes up as a pop up. but I have a close button on the survey WebPart which needs to close the popup.

The Close button is :

 <button type="button" OnClick="ClosePopup()" >Close</button>

and Here is the script:

      function ClosePopup() {
          document.cookie = "PostBack";
          SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, null);
         // SP.UI.ModalDialog.close(dialogResult);

        //  self.close();


As you see I tried self.close(); and SP.UI.ModalDialog.close(dialogResult); not working.

Please advise.


  • Change it to "onclick" instead of "OnClick".
    – Boland
    Commented Oct 22, 2014 at 7:31
  • Do you open the modal dialog with javascript? If not, you can't close it with javascript. Ref: stackoverflow.com/a/19792805/286244
    – Benny Skogberg
    Commented Oct 22, 2014 at 7:38

1 Answer 1


It is just fixed when I used the below JavaScript to close the popup

    function ClosePopup() {
              document.cookie = "Saved";

and the button is

 <asp:Button ID="Submit" runat="server" CSSClass="Btn" Text="Submit" OnClick="Submit_Click" OnClientClick="SavedInlist()"  />

It is working fine now.

Also I realized SharePoint 2013: ExecuteOrDelayUntilScriptLoaded not executing after page publish, To make sure the SharePoint script file 'sp.js' is loaded before my function runs. I changed

ExecuteOrDelayUntilScriptLoaded(ShowDialog, "sp.js");


SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ShowDialog);

Now it is running when page is published.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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