Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

With IE 10 and Chrome 26 on Win7 x64 (Host1) no issues are appearing, except Firefox 20.

With IE IE 8 on Win7 x64 (Host2) and IE 10 on Windows Server 2012 x64 (Host3), there is the following issue I noticed with SharePoint 2013:

SCRIPT5007: Unable to get property 'showModalDialog' of undefined or null reference

and when I look into the code: SP.UI.ModalDialog (for which I call: SP.UI.ModalDialog.showModalDialog(optDict);) seems to be undefined here. Only few properties of SP.UI is available on Host2 and Host3, while on Host1 there are much more of these.

There is jQuery .click() event on some HTML element that triggers the function to display ModalWindow.

So it's as follows:

<script type="text/javascript">

    $(document).ready(function() {

        $("#some-a-element").click(function uploadMyFile(directory)) {

            // something...

            optDict = {
                width: 800,
                height: 500,
                url: someUrl,
                title: "Upload your file"
            };

            SP.UI.ModalDialog.showModalDialog(optDict);

            // something else
        });

        // sumthing even here
    });

    // more code...

 </script>

Looking for the solution over the internet, I've found out (in here) that this might be connected to lazy loading of SP.js and proper usage of ExecuteOrDelayUntilScriptLoaded function should solve the issue.

So in my case it should be something like

ExecuteOrDelayUntilScriptLoaded(uploadMyFile, 'SP.js');

...I guess... Right?

However, I have no idea how to achieve this with .click() event handler presented above - and how to arrange it in this code structure. Tried few combinations that came into my mind, but without any success...

Any idea how to do so?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Just wrap your code inside the uploadMyFile function into ExecuteOrDelayUntilScriptLoaded, as shown below:

<script type="text/javascript">

    $(document).ready(function() {

        $("#some-a-element").click(function uploadMyFile(directory)) {

            ExecuteOrDelayUntilScriptLoaded(function() {    

                // something

                optDict = {
                    width: 800,
                    height: 500,
                    url: someUrl,
                    title: "Upload your file"
                };

                SP.UI.ModalDialog.showModalDialog(optDict);

                // something else

            }, "sp.js");

        });

        // sumthing even here
    });

    // more code...

 </script>

Notice: There is one more gotcha here, introduced in SP2013. For published pages, the ExecuteOrDelayUntilScriptLoaded will not work. Instead, you should use SP.SOD.executeFunc, which has a slightly different syntax.

Details on this can be found here: ExecuteOrDelayUntilScriptLoaded not executing after page publish

share|improve this answer
    
You're right! executeFunc is something I was looking for. With this one everything works fine. Thanks a lot! –  user1029968 May 21 '13 at 12:09
    
You're welcome :) –  Andrey Markeev May 21 '13 at 12:55
add comment
  var options = {
                        url : $().SPServices.SPGetCurrentSite() + "/_layouts/15/KoellischFrameworkEMailForm/EMailVorlageBefuellen.aspx?id=" + $(el).attr("ID").replace("VorlagenTableRowId", "") + "&ElementId=" + elementId + "&ListName=" + listName
                    };
                     options.dialogReturnValueCallback = Function.createDelegate(null, function (result, value) {
                if (result == '1') {
                    var signatur = setzeSignatur();
                    $('input[id*=TextBoxBetreff]').val(value[0]);
                    if (signatur == null) {
                        $("#jhtm").contents().find('body').html(value[1]);
                    } else {
                        $("#jhtm").contents().find('body').html(value[1] + "<br />" + signatur);
                    }
                    $('#Vorlagen').hide();
                    $('#vorlagenButton').show();
        }
    });
                    SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
                });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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