How do I set a file attachment to open target="_blank"?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
pkchoo's picture

I hope there is a simple answer to my question...

How do I set a file attachment to open target="_blank" instead of "_self"?

I have a file attachment on an article content type, but I see no way of allowing that to open up on a separate page.

Thank you,
Joe

Comments

Are you using Upload module?

christefano's picture

Are you using Upload module? FileField?

For Upload module, you can use a _upload_attachments theme override. There's an example at https://drupal.org/node/266130#comment-870290

If you're looking to override the output from Views, stevenator posted a solution at https://drupal.org/node/562294#comment-4721938

Thank you for the reply. I

pkchoo's picture

Thank you for the reply.

I forgot to mention, I'm using Drupal 7. I don't see the upload module or filefield module anywhere. Are those functionalities a part of core?

Does your suggestion still work in D7?

Thank you,
Joe

for

jQuery can help with this

pdumais42's picture

I do this on the ModernMom web site. Forcing certain menu items to open in a new window. The same technique works for any link on the page that can be selected via ID or CLASS.

I add the following jQuery into the page (or in the theme if it affects every page).

$(document).ready(function(){
  $('#menu-3475 a').each(function(){$(this).attr({ target: "_blank" });});
  $('li.menu-3475 a').each(function(){$(this).attr({ target: "_blank" });});
  $('#menu-33430 a').each(function(){$(this).attr({ target: "_blank" });});
  $('li.menu-33430 a').each(function(){$(this).attr({ target: "_blank" });});
});

You can see this working by clicking on the SHOP link at http://www.modernmom.com/

As a bonus, I will share with you how I get certain files to download rather than appear in a new window. The use case is when we provide a CLICK HERE TO DOWNLOAD AS PDF. We need to tell the web server (Apache2 in my case) that files in a specific location should be delivered as binary attachments. Then, configure your Drupal content type / field settings to put the files in this "special" location.

This is what I added to our server config.

    <Directory "/var/www/sites/default/files/downloads/">
        ForceType application/octet-stream
        Header set Content-Disposition attachment
    </Directory>

Now, any files that are in this directory will download without opening a new window. You can see it in action here: http://www.modernmom.com/slideshow/download-calendars-free-and-helpful-o... (Click on the Download link).

Cheers!

another solution with jQuery

godler's picture

Add to your_theme.info file:

scripts[] = js/javascript.js

In this js file:

(function ($) {
$(document).ready(function() {
$('.field-download-document a').attr({ target: "_blank" });
});
}(jQuery));

FileField Target for Drupal7

Pixelstack's picture

FileField Target works for me! It adds a display formatter which you can select in your Content Type's configuration, under "Manage display". Still trying to figure out how to open the files in a new tab in the block view though. (Ah! that same formatter is available in the view settings of the File.)

http://drupal.org/project/filefield_target

Confirmation

zamorta's picture

It works! Even in views.