5

I've got a view and one of the fields is displayed as a link. (Output this field as a link is cheked). But now i want those links to have target=_blank attribute.

How can i achieve this?

4

When you check Output this field as a link there is a Target input box available (Further down). In that input box you can write _blank. (D7)

2

edit: I just saw the answer whitefleaCH gave you - I never use the output settings as i prefer to style my views using templates to clean up the code - so i didn't know that option existed, though I presumed it did..


You can either use the themeing part of view and create your own template for the row style and simply hardcode the link attribute into it there (click the 'theme: information' part in the advanced settings of your view to see how to name your theme template).

Or which is probably easier - depending on how much themeing you'll be doing to your view - is to use jquery by doing the following:

Create a .js file and paste the following in into it (create a new textfile and save as yourfile.js):

(function($) {

$(document).ready(function() {

      $('.CLASS a[href]').attr({
          target: "_blank", 
          title: "Opens in a new window"
        })

});

}(jQuery));

Change the CLASS bit with the class surrounding your link and save the file.

Upload your YOURFILE.js to your theme directory (eg: sites/all/themes/YOURTHEME/) Open YOURTHEME/template.php file and tell it to load your .js file by adding the following line

drupal_add_js(drupal_get_path('theme', 'YOURTHEME').'/assets/js/YOURFILE.js');

Clear your cache and all links surrounded by

  <div class="CLASS"><a href="">Link</a></div>

will automatically open in a new window.

Of course you can do a lot more with the above script, like add a special class to the links so you can use .css to style them differently, or append some symbol or image etc etc ...

I hope this helps.

  • "CLASS"? What an "imaginative" name! :D Btw. target="_blank" causes XHTML Strict-validation problems, so maybe your method is the right one instead of "burning" it into the HTML-output. – Sk8erPeter Nov 10 '11 at 10:53
  • haha, yes, I was trying to be clear as there is a dot before it ;-) – tecjam Nov 10 '11 at 11:01
  • don't think JS is ever a good option for this – Scott Lawrence Jul 29 '13 at 2:34
1

Linked Field module with Tokens.

Just brilliant!

Using of the Linked Field module example

After that in your View you may select Show:Content | Teaser (Teaser or any other preferable display)

The same but kinda verbose way give us the Image Link Formatter module, but only for images.

@whitefleaCH gives good solution too! (only for Views module)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy