SharePoint 2010:创建模式对话框时出现 JavaScript 错误?

发布于 2024-11-14 12:12:51 字数 619 浏览 5 评论 0原文

由于某种原因,我的 SharePoint 模式对话框无法正常工作。我得到的错误是这样的:

  • 在 Firefox 中:SP.UI.$create_DialogOptions 不是函数
  • 在 IE 中:对象不支持此属性或方法

这是我的代码:

var options = SP.UI.$create_DialogOptions();
options.width = 525;
options.height = 300;
options.url = '/_layouts/mywork/richtexteditor.aspx';
options.dialogReturnValueCallback = Function.createDelegate(null, function (result, value)
{
    alert(result + value);
});

SP.UI.ModalDialog.showModalDialog(options);

有趣的是,当我在 Firebug 中检查 SP.UI 时,我没有看到所有方法和属性。

注意:我使用的是标准 Web 部件(非视觉部件),而不是应用程序页面。

For some reason, my SharePoint's modal dialog doesn't work properly. The error I get is this:

  • In Firefox: SP.UI.$create_DialogOptions is not a function
  • In IE: Object doesn't support this property or method

Here is my code:

var options = SP.UI.$create_DialogOptions();
options.width = 525;
options.height = 300;
options.url = '/_layouts/mywork/richtexteditor.aspx';
options.dialogReturnValueCallback = Function.createDelegate(null, function (result, value)
{
    alert(result + value);
});

SP.UI.ModalDialog.showModalDialog(options);

Interestingly, when I inspect the SP.UI in Firebug, I don't see all the methods and properties.

NOTE: I am using standard Webpart (not visual) and not an application page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┊风居住的梦幻卍 2024-11-21 12:12:51

问题是所需的 SharePoint JavaScript“库”尚未加载。(SharePoint 2010 JS 有点混乱,命名空间/等来自各地 - 问题是新的“按需”加载进一步复杂化)。

使用SP2010模态对话框界面(包括$create_DialogOptionsshowModalDialog)需要加载的库是“sp.js”。

确保“sp.js”已加载:

ExecuteOrDelayUntilScriptLoaded(function () {
  // do modal dialog stuff in here (or in another function called from here, etc.)
}, "sp.js")

回调函数仅在“sp.js”(包括 SP.UI.ModalDialog 内容)被保证后调用被加载(如果出现加载错误,它可能永远不会被调用)。

这也可以通过使用 sp.js 并指定 OnDemand 来解决,但我不能保证它:(我认为这种方法可能存在问题,但我不记得为什么我刚刚查看的项目中没有使用它。)

<SharePoint:ScriptLink runat="server" Name="sp.js" OnDemand="true" Localizable="false" />

参见SPSOD 了解更多详细信息/信息。

快乐编码。

The problem is that the required SharePoint JavaScript "Library" hasn't been loaded. (The SharePoint 2010 JS is a good bit of a mess and namespaces/etc. come from all over -- the matter is further complicated with the new "on demand" loading).

The library that needs to be loaded to use SP2010 Modal Dialog interface (including the $create_DialogOptions and showModalDialog) is "sp.js".

To ensure "sp.js" is loaded:

ExecuteOrDelayUntilScriptLoaded(function () {
  // do modal dialog stuff in here (or in another function called from here, etc.)
}, "sp.js")

The call-back function is only invoked after "sp.js" (including the SP.UI.ModalDialog stuff) is guaranteed to be loaded (and it may never be called if there is a loading error).

This could also likely be solved with using a <ScriptLink> to sp.js with OnDemand specified, but I can't guarantee it: (I think there may have been issues with this approach, but I can't recall why it's not used in the project I just looked at.)

<SharePoint:ScriptLink runat="server" Name="sp.js" OnDemand="true" Localizable="false" />

See SPSOD for some more details/information.

Happy coding.

妄断弥空 2024-11-21 12:12:51

对我来说它是这样工作的:
ExecuteOrDelayUntilScriptLoaded(function () {}, "sp.js")

以及:

<SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="sp.js" 
    OnDemand="false" Localizable="false" LoadAfterUI="true"/>

For me it worked like this:
ExecuteOrDelayUntilScriptLoaded(function () {}, "sp.js")

and:

<SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="sp.js" 
    OnDemand="false" Localizable="false" LoadAfterUI="true"/>
迷雾森÷林ヴ 2024-11-21 12:12:51

发现Adela 和user166390 的方法在旧的IE 7-8 中都失败了。似乎页面没有完全解析并尝试通过对话框的 iframe 进行修改,这对那些 IE 来说是不好的。对于我的情况 - 我需要在应用程序页面中自动弹出对话框 - 我用 next

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:SPPageManager ID="SPPageManager1" runat="server" />
    <script type="text/javascript">
        var ShowDialog = function () {
            var options = {
                url: '/_login/default.aspx,
                title: 'Title, Description, and Icon',
                width: 640,
                height: 400,
                dialogReturnValueCallback: function(dialogResult, returnValue) {
                    window.location.replace(returnValue);
                }
            };

            SP.UI.ModalDialog.showModalDialog(options);
        };

        ExecuteOrDelayUntilScriptLoaded(ShowDialog, "sp.ui.dialog.js");
    </script>
</asp:Content>

这个小东西

<SharePoint:SPPageManager ID="SPPageManager1" runat="server" />

注册了所有 SP javascript 来修复它。

该方法可在 MSDN 论坛

Found that both Adela's and user166390's approaches fail in old IEs 7-8. Seems that page is not completely parsed and tried to be modified by dialog's iframe and this is bad for those IEs. For my case - I need to auto-popup dialog in application page - I fixed it with next

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:SPPageManager ID="SPPageManager1" runat="server" />
    <script type="text/javascript">
        var ShowDialog = function () {
            var options = {
                url: '/_login/default.aspx,
                title: 'Title, Description, and Icon',
                width: 640,
                height: 400,
                dialogReturnValueCallback: function(dialogResult, returnValue) {
                    window.location.replace(returnValue);
                }
            };

            SP.UI.ModalDialog.showModalDialog(options);
        };

        ExecuteOrDelayUntilScriptLoaded(ShowDialog, "sp.ui.dialog.js");
    </script>
</asp:Content>

This little thing

<SharePoint:SPPageManager ID="SPPageManager1" runat="server" />

registers all SP javascripts.

The approach was found on MSDN forums.

‖放下 2024-11-21 12:12:51

您可以通过使用选项的通用对象而不是 DialogOptions 类来解决此问题。这意味着您必须像这样编写选项:

//Using a generic object.
var options = {
    title: "My Dialog Title",
    width: 400,
    height: 600,
    url: "/_layouts/DialogPage.aspx" };

有关使用它的更多信息,请访问:
http://msdn.microsoft.com/en -us/library/ff410058%28v=office.14%29.aspx
并查看示例。

You can fix this issue by using a generic object for option instead of the DialogOptions class. that mean you have to write option like this:

//Using a generic object.
var options = {
    title: "My Dialog Title",
    width: 400,
    height: 600,
    url: "/_layouts/DialogPage.aspx" };

for more information about using it, visit:
http://msdn.microsoft.com/en-us/library/ff410058%28v=office.14%29.aspx
and see the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文