none
How can I change the onclick event javascript associated with an Item in a document library?

    Question

  • The default onclick handler for a document library item links is (item dependent):

    onclick="return DispEx(this,event,'TRUE','FALSE','TRUE','SharePoint.OpenDocuments.3','0','SharePoint.OpenDocuments','','','','164','0','0','0xb008431061')"
    

     

    How can I change this javascript code for the onclick event so that it does something different ?

    Thursday, December 08, 2011 3:34 PM

Answers

  • Hi  Paul Fr:

     

    As your question ,you can use following code based on Falco Ostermann’s reply:

        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js"></script>

        <script type="text/javascript"> 

            $(document).ready(function () {

     

                $("td.ms-vb-icon > a").each(function () {

                    try {

                        var yourElement = $(this);

                        var filename;

                        var itemid;

                        var str = yourElement.attr("onclick").toString();

                        //sample:onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','SharePoint.OpenXMLDocuments.3','InfoPath.Document.3','1http:\u002f\u002flhan-pc1:8081\u002f_layouts\u002fFormServer.aspx?XmlLocation=\u002flib\u002fForm3.xml','','1073741823','0','0','0x7fffffffffffffff')"

                        filename = str.split(',')[9];

                        itemid = str.split(',')[11];

                        if (yourElement != null) {

                            yourElement.removeAttr("onclick");

                            yourElement.unbind("click");

                            yourElement.attr({ href: "javascript:yourCustomFuntion(" + filename + "," + itemid + ")" });

                        }

                    }

                    catch (e) {

     

                    }

                });

            });

            function yourCustomFuntion(filename, itemid) {

                alert("testSuccessed");

            }

        </script>

    Then add a Content Editor Web Part into your list page. Edit the Part ,copy the script into the Web Part  

     

    Thanks,

    Lhan Han

    • Marked as answer by Paul Fr Friday, December 16, 2011 5:27 PM
    Thursday, December 15, 2011 3:53 AM
    Moderator

All replies

  • Hi,

    in your case i would change the onclick event with jQuery. Because it is much easier than normal javascript and you spent a lot of time. 

     

    var yourElement = $(id of your element);
    if (yourElement != null) {
    yourElement.removeAttr("onclick");
    yourElement.unbind("click");
    yourElement.bind('click', yourCustomFunction);
    }
    
    function yourCustomFuntion(){
    alert("jQuery rocks!");
    }
    

     


    Do you want to change this event only for one linkItem or for the whole list?

     


    Falco - http://www.msscorner.de
    Thursday, December 08, 2011 5:19 PM
  • Looking good ... I want it for the whole list with the filename or the itemid as a parameter.
    • Edited by Paul Fr Thursday, December 08, 2011 5:24 PM
    Thursday, December 08, 2011 5:23 PM
  • $("td.ms-vb-title > div a").each(function(){
    var yourElement = $(this);
    if (yourElement != null) {
    yourElement.removeAttr("onclick");
    yourElement.unbind("click");
    yourElement.bind('click', yourCustomFunction);
    }
    });
    
    function yourCustomFuntion(){
    alert("jQuery rocks!");
    }


    I dont know whether this each statement work for you. Because the html DOM is in every Listview a bit different. So you have to modified this statement. So that you get every a element you want.

    some instructions:

    Falco - http://www.msscorner.de

    Thursday, December 08, 2011 7:05 PM
  • Hi  Paul Fr:

     

    As your question ,you can use following code based on Falco Ostermann’s reply:

        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js"></script>

        <script type="text/javascript"> 

            $(document).ready(function () {

     

                $("td.ms-vb-icon > a").each(function () {

                    try {

                        var yourElement = $(this);

                        var filename;

                        var itemid;

                        var str = yourElement.attr("onclick").toString();

                        //sample:onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','SharePoint.OpenXMLDocuments.3','InfoPath.Document.3','1http:\u002f\u002flhan-pc1:8081\u002f_layouts\u002fFormServer.aspx?XmlLocation=\u002flib\u002fForm3.xml','','1073741823','0','0','0x7fffffffffffffff')"

                        filename = str.split(',')[9];

                        itemid = str.split(',')[11];

                        if (yourElement != null) {

                            yourElement.removeAttr("onclick");

                            yourElement.unbind("click");

                            yourElement.attr({ href: "javascript:yourCustomFuntion(" + filename + "," + itemid + ")" });

                        }

                    }

                    catch (e) {

     

                    }

                });

            });

            function yourCustomFuntion(filename, itemid) {

                alert("testSuccessed");

            }

        </script>

    Then add a Content Editor Web Part into your list page. Edit the Part ,copy the script into the Web Part  

     

    Thanks,

    Lhan Han

    • Marked as answer by Paul Fr Friday, December 16, 2011 5:27 PM
    Thursday, December 15, 2011 3:53 AM
    Moderator
  • Hi ... getting there: I have modified the selector as shown below and am using this code injected into the page based on an activated feature. I could not get the doc ID so I am using the doc path and name instead which gives what I need. Now when a user clicks the doc link a custom page within the List definition is displayed .....

    $(document).ready(function () {
    
     
    
                $('table [class="ms-vb-title"]').find('tr').find('a').each(function () {
    
                    try {
    
                        var yourElement = $(this);
    
                        var filename;
    
                        var itemid;
    
                        var str = yourElement.attr("href").toString();
    
                        //sample:onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','','0','SharePoint.OpenXMLDocuments.3','InfoPath.Document.3','1http:\u002f\u002flhan-pc1:8081\u002f_layouts\u002fFormServer.aspx?XmlLocation=\u002flib\u002fForm3.xml','','1073741823','0','0','0x7fffffffffffffff')"
    
                        filename = str.split('/')[str.split('/').length-1];
                        str = str.replace(filename, 'viewPropertiesUser.aspx?Name=' + filename);
    
                        if (yourElement != null) {
    
                            yourElement.removeAttr("onclick");
    
                            yourElement.unbind("click");
    
                            yourElement.attr({ href: "javascript:yourCustomFuntion('" + str + "')" });
    
                        }
    
                    }
    
                    catch (e) {
    
     
    
                    }
    
                });
    
            });
    
            function yourCustomFuntion(str) {
    
                alert("testSuccessed");
    
            }

     

    • Edited by Paul Fr Friday, December 16, 2011 5:30 PM
    Friday, December 16, 2011 4:43 PM