Reputation: 406
I have a Sharepoint 2010 page with a link that opens a web-based version of Excel. I want to strip the onclick and onmousedown events from the link and turn it into a regular link that simply downloads the excel file. I tried using this jquery code to strip the event handlers from the 3rd item in the 1st row of the table (see code below), but it didn't work. Is it possible to manipulate these javascript events to create a regular link?
$(window).load(function() {
$(".ms-vb-title a").attr("onmousedown",alert("Test"));
});
<table summary="ManagerResourcesFiles " o:webquerysourcehref="/wg/ProdPayroll/_vti_bin/owssvr.dll?CS=65001&XMLDATA=1&RowLimit=0&View={25D77A0C-2CB2-4B32-8CEC-9899E9A64193}" width="100%" border="0" cellspacing="0" dir="none" onmouseover="EnsureSelectionHandler(event,this,48)" cellpadding="1" id="onetidDoclibViewTbl0" class="ms-listviewtable" xmlns:o="urn:schemas-microsoft-com:office:office" handledeleteinit="true">
<tbody>
<tr class="ms-itmhover" iid="48,3,0" setedgeborder="true">
<td class="ms-vb-itmcbx ms-vb-firstCell"><input type="checkbox" class="s4-itm-cbx" title="Paymaster Roster"></td>
<td class="ms-vb2"><div><img src="/wg/ProdPayroll/icon/excel.png"></div></td>
<td height="100%" onmouseover="OnChildItem(this)" class="ms-vb-title"><div class="ms-vb itx" onmouseover="OnItem(this)" ctxname="ctx48" id="3" field="LinkFilename" perm="0x7fffffffffffffff" eventtype=""><a onfocus="OnLink(this)" href=" /wg/ProdPayroll/_layouts/xlviewer.aspx?id=/wg/ProdPayroll/ManagerResourcesFiles/Paymaster%20Roster.xlsx&Source=https%3A%2F%2Fepwork%2Eep%2Ecorp%2Fwg%2FProdPayroll%2FSitePages%2FManagerResources%2Easpx&DefaultItemOpen=1&DefaultItemOpen=1" onmousedown="return VerifyHref(this,event,'0','SharePoint.OpenDocuments','/wg/ProdPayroll/_layouts/xlviewer.aspx?id=/wg/ProdPayroll/ManagerResourcesFiles/ Employee %20Roster.xlsx')" onclick="return DispEx(this,event,'TRUE','FALSE','FALSE','SharePoint.OpenDocuments.3','0','SharePoint.OpenDocuments','','1https://epwork.ep.corp/wg/ProdPayroll/_layouts/xlviewer.aspx?id=/wg/ProdPayroll/ManagerResourcesFiles/Paymaster%20Roster.xlsx','','750','0','0','0x7fffffffffffffff','','')">Employee Roster</a></div><div class="s4-ctx" onmouseover="OnChildItem(this.parentNode); return false;" style="top: 149px; left: 831px; height: 43px; line-height: 43px; margin: 0px;"><span> </span><a onfocus="OnChildItem(this.parentNode.parentNode); return false;" onclick="PopMenuFromChevron(event); return false;" href="javascript:;" title="Open Menu"><img src="/_layouts/images/ecbarw.png" alt="Open Menu" style="visibility: hidden;"></a><span> </span></div></td>
<td class="ms-vb2"><nobr>4/11/2019 4:20 PM</nobr></td>
<td class="ms-vb-user ms-vb-lastCell"><span class="ms-imnSpan"><img name="imnmark" class="ms-imnImg" title="" border="0" height="12" width="12" src="/_layouts/images/blank.gif" alt="No presence information" sip="[email protected]" id="imn_313,type=smtp"><a onclick="GoToLink(this);return false;" href="/wg/ProdPayroll/_layouts/userdisp.aspx?ID=750">John Smith</a></span></td></tr>
</tbody
Upvotes: 3
Views: 283
Reputation: 12900
It appears that you cannot unbind an onmousedown
event with off
or unbind
. However, you can destroy that by just setting the attribute to null
$('.ms-vb-title a').attr('mousedown', null); // <-- Remember to put quotes around your selector
If this isn't working and the event is still attached to it, I would verify your selector is correct. I remember when I worked in SharePoint, the DOM was a complete mess (duplicate ids all over the place among other things) and I would always be fighting to figure out the correct selector.
If you're looking for advice on the downloading part, you can easily convert a link into a download object (or create a hidden one).
let csvData = `data:text/csv;charset=utf-8, ${myCsvData}`;
let encodedUri = encodeURI(csvData);
let link = document.createElement('a');
link.href = encodedUri;
link.download = 'mycsv.csv';
document.body.appendChild(link);
link.click(); // <-- will trigger the actual download process
function test() {
alert('tst');
}
$(document).ready(() => {
const button = $('button');
// button.off('onmousedown'); // <-- wont work
// button.unbind('onmousedown'); // <-- wont work
// button.off('mousedown'); // <-- wont work
// button.unbind('mousedown'); // <-- wont work
// button.attr('onmousedown', null); // <-- uncomment to kill onmousedown event
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onmousedown="test()">
Test
</button>
Upvotes: 3