将Javascript函数附加到OOTB功能区按钮

时间:2011-10-03 16:15:36

标签: sharepoint sharepoint-2010

我想将自定义javascript功能附加到开箱即用的“下载副本”功能区按钮。这是为通过功能区按钮完成的文件下载提供分析。

我尝试了这段代码,但它似乎不起作用:

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
    </script>
    <script>
    _spBodyOnLoadFunctionNames.push("Trackdownloads");

    function Trackdownloads(){

        debugger;
        $("a[id='Ribbon.Documents.Copies.Download-Large']").live( "click", 
                  function() {
                      alert('hello');
                  }
            );

    }

    </script>

知道如何让它发挥作用吗?

2 个答案:

答案 0 :(得分:1)

找到解决我问题的方法。 实际上,将javascript函数附加到按钮不是实现此功能的正确方法。 正确的实现是用自定义按钮替换OOTB按钮,并调用自定义javascript函数来执行所需的操作。

http://msdn.microsoft.com/en-us/library/ff407619.aspx

答案 1 :(得分:0)

如果您确实要覆盖功能区中的按钮。创建自定义页面组件是个好主意。例如:

Type.registerNamespace('Company.Project.Ribbon.PageComponent');

Company.Project.Ribbon.PageComponent = function (PageComponentId) {
    this._pageComponentId = PageComponentId;
    Company.Project.Ribbon.PageComponent.initializeBase(this);
}

Company.Project.Ribbon.PageComponent.prototype =
{
    _pageComponentId: "PageComponentIDHolder",

    getId: function () {
        return this._pageComponentId;
    },
    init: function () {
        this._myCommandList = ['DownloadCopy'];
        this._myHandledCommands = {};
        this._myHandledCommands['DownloadCopy'] = Function.createDelegate(this, this.CMD1_Handler);
    },
    getFocusedCommands: function () {
        return this._myCommandList; 
    },
    getGlobalCommands: function () {
        return this._myCommandList; 
    },
    canHandleCommand: function (commandId) {
        var canHandle = this._myHandledCommands[commandId];

        if (canHandle)
            return true;
        else
            return false;
    },
    handleCommand: function (commandId, properties, sequence) {
        return this._myHandledCommands[commandId](commandId, properties, sequence);
    },
    isFocusable: function () {
        return true;
    },
    CMD1_Handler: function (commandId, properties, sequence) {
        alert('Download a copy-button was clicked');
    }
}

Company.Project.Ribbon.PageComponent.registerClass('Company.Project.Ribbon.PageComponent', CUI.Page.PageComponent)
NotifyScriptLoadedAndExecuteWaitingJobs("/_layouts/custompagecomponents/PageComponent.js");



function init2() {
    var instance = new Company.Project.Ribbon.PageComponent("ComponentID");
    SP.Ribbon.PageManager.get_instance().addPageComponent(instance);
}

function init1() {
    ExecuteOrDelayUntilScriptLoaded(init2, 'sp.ribbon.js');
}
ExecuteOrDelayUntilScriptLoaded(init1, '/_layouts/custompagecomponents/PageComponent.js');