0

I am trying to open list item attachments in a new tab (when user clicks on an attachment in display form)

I am using this piece of jQuery/javascript in my custom display form to do that:

$(document).ready(function(){
        $("table#idAttachmentsTable a").click(function (event) {
            event.preventDefault();
            var url=$(this).attr('href');
            window.open(url, '_blank');
        });
    });

Now when user clicks on the attachment, it opens in a new tab, but when you close that new tab and return to main tab, it still opens an attachment there (again).

What am I missing to stop it from doing this?

I've tried adding return false; at the end, but it doesn't change anything

2
  • Which SharePoint Version & Browser are you using?
    – Hardik
    May 16, 2016 at 12:08
  • It's a Sharepoint Online environment, I need this to work on IE, Chrome and Firefox, it doesn't work on any of these at the moment :/ @Hardik
    – Gintas K
    May 16, 2016 at 12:10

1 Answer 1

4

Just resolved my issue.

I referred to this question's answer and added

.attr('onclick', '')

This clears the onclick attribute in the html.

So my code now looks like this:

$(document).ready(function(){
        $("table#idAttachmentsTable a").attr('onclick', '').click(function (event) {
            event.preventDefault();
            var url=$(this).attr('href');
            window.open(url, '_blank');
        });
    });
3
  • how would it be done for .PDF files / links only.??
    – Bucki
    Apr 11, 2018 at 14:57
  • Where did you put this piece of jQuery/javascript?
    – A.Miller
    Oct 26, 2018 at 15:39
  • @A.Miller inside the DispForm.aspx file, using SharePoint Designer
    – Gintas K
    Oct 29, 2018 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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