How do I make my attachments open in a new window or tab?

Still need help?

The Atlassian Community is here for you.

The information in this page relates to customisations. Consequently, Atlassian Support cannot guarantee to provide any support for the steps described on this page as customisations are not covered under Atlassian Support Offerings. Please be aware that this material is provided for your information only and that you use it at your own risk.

A free plugin is available that reproduces some of this functionality.

Purpose

To have attachments open up in a new window or tab when clicking on them.

Solution

You need to add a TARGET = "_blank" to the <a href> HTML tag.

The A element used in HTML denotes an anchor which is a hypertext link.
The HREF attribute specifies a hypertext link to another resource, such as an HTML document or a JPEG image.
The TARGET attribute is used with frames to specify the frame in which the link should be rendered. If no frame with such a name exists, the link is rendered in a new window unless overridden by the user. Special frame names begin with
an underscore. The frame used in this document is the _blank which renders the link in a new, unnamed window.

For example, by using the HTML code below, clicking on the link "a new window" will open the "newwindow.html" page in a new window:

<A href="newwindow.html" _TARGET="_blank"_>a new window</A>

Open attachments listed for a Space

To open the attachments listed from the Browse Space->Attachments tab, in a new window, the file <CONFLUENCE-INSTALL>/confluence/pages/listattachmentsforspace.vm has to be modified. Below are the listed steps:

1. Locate the following block of code in the listattachmentsforspace.vm file:

#foreach ($attachment in$results)
#set ($labels =$attachment.labels)
#set ($labelable =$attachment)
<tr id="attachment_$!attachment.id"> <td><a name="$!generalUtil.urlEncode($!attachment.content.displayTitle)-attachment-$!generalUtil.urlEncode($!attachment.fileName)">#parse ("/pages/includes/attachment_icon.vm")</a> <a href="$req.contextPath$!attachment.downloadPathWithoutVersion">$generalUtil.shortenString($attachment.fileName, 50)</a></td> <td>$!attachment.niceFileSize</td>
<td>#usernameLink($!attachment.creatorName)</td> <td>$dateFormatter.formatDateTime($!attachment.creationDate)</td> <td>$dateFormatter.formatDateTime($!attachment.lastModificationDate)</td> <td>#parse("/labels/labels-editor-content.vm")</td> <td class="attachedto">#contentLink2 ($!attachment.getContent() true false)</td>
</tr>
#end
2. In the line below:

<td><a name="$!generalUtil.urlEncode($!attachment.content.displayTitle)-attachment-$!generalUtil.urlEncode($!attachment.fileName)">#parse ("/pages/includes/attachment_icon.vm")</a> <a href="$req.contextPath$!attachment.downloadPathWithoutVersion">$generalUtil.shortenString($attachment.fileName, 50)</a></td>

add the parameter target="_blank" to the <a href> HTML tag, which will cause the URL specified in the href parameter to open in a new window or a new tag depending upon the option set in the browser. So the line above will be modified to:

<td><a name="$!generalUtil.urlEncode($!attachment.content.displayTitle)-attachment-$!generalUtil.urlEncode($!attachment.fileName)">#parse ("/pages/includes/attachment_icon.vm")</a> <a href="$req.contextPath$!attachment.downloadPathWithoutVersion" target="_blank">$generalUtil.shortenString($attachment.fileName, 50)</a></td>

Open attachments listed for a Page

To open the page attachments listed from the Page's Attachment(s) tab, in a new window, the file <CONFLUENCE-INSTALL>/confluence/pages/includes/attachments-table.vm has to be modified. Below are the listed steps:

1. Locate the following block of code in the attachments-table.vm file:

<td class="filename-column">
#parse ("/pages/includes/attachment_icon.vm")
<a class="filename" href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")" title="$generalUtil.htmlEncodeAndReplaceSpaces($attachment.fileName)">$generalUtil.htmlEncode($attachment.fileName) </a> </td> 2. In the line below: <a class="filename" href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")"
title="$generalUtil.htmlEncodeAndReplaceSpaces($attachment.fileName)">

add the target="_blank" parameter to the <a href> HTML tag which will cause the URL specified in the href parameter to open in a new window.

<a class="filename" href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")" title="$generalUtil.htmlEncodeAndReplaceSpaces($attachment.fileName)" target="_blank"> 3. Locate the following block of code in the attachments-table.vm file: <td class="filename-column"> <a class="filename" href="$generalUtil.htmlEncode("$req.contextPath$attachmentVersion.downloadPath")">#if ($velocityCount == 1)$action.getText("attach.file.version.current", $attachmentVersion.getVersion())#else$action.getText("attach.file.version.dated", $attachmentVersion.getVersion())#end</a> </td> and add the target="_blank" parameter to the <a href> HTML tag which will cause the URL specified in the href parameter to open in a new window. <td class="filename-column"> <a class="filename" href="$generalUtil.htmlEncode("$req.contextPath$attachmentVersion.downloadPath")" target="_blank">#if ($velocityCount == 1)$action.getText("attach.file.version.current", $attachmentVersion.getVersion())#else$action.getText("attach.file.version.dated", \$attachmentVersion.getVersion())#end</a>
</td>

After making these changes, you'll need to restart Confluence for the changes to take effect.