I know the external links have been discussed many times, but it always comes down to administator/designer choice what is best for users. Yes, (the experienced one) can open the link in a new window/tab. Still, I believe it would be a great feature to have a block with a checkbox "Open external links in a new window". Unfortunately I don't have the skills to do it myself.

Comments

kalin_s’s picture

Christmas came early this year - :) or :(
----------
Working on a bilingual mini-portal - www.archeologic.net

kbahey’s picture

What you need is not a module, but rather a filter. A filter manipulates the text of a node before it is output to the browser.

There are several examples in the modules Download section, look at image filter, code filter, ...etc.

--
Consulting: 2bits.com
Personal: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba

kalin_s’s picture

How would this filter fit in a block with the checkbox?
The filter will screen the "http//...." links?
----------
Working on a bilingual mini-portal - www.archeologic.net

kbahey’s picture

I thought you meant that as a checkbox for the admin to force all external limnks to open in a new window.

Now that you want the end user to select that, including anonymous users, a filter may not do the trick.

--
Consulting: 2bits.com
Personal: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba

connellc’s picture

External Links, for D7, allows the admin to checkmark the configuration to force a pop-up to show. The pop-up will say, like "are you sure you wanna do this - open up a new window? We're not responsible for their content." "Course, you can have the pop-up say anything you want.

laura s’s picture

On maybe WordPress (?) I've seen a visitor-optional checkbox to set whether you want the links to open into a new window or not. I'm not sure, but I would think it'd have to targe only off-site links or navigating the site would be a nightmare. Also, it seemed to be a persistent setting, but always available for change.

Something like this, which would have a block where the user could set this, might be nice. +1 from me.

.:| Laura • pingV |:.

_____ ____ ___ __ _ _
Laura Scott :: design » blog » tweet

Steven’s picture

Paste this in a file called newwindow.module and install it as a module:


function newwindow_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(0 => t('New window filter'));

    case 'description':
      return t('Forces external links in submitted content to open in a new window.');

    case "process":
      return preg_replace('!<a (?:href=["\']?http)!i', '<a target="_blank" ', $text);

    default:
      return $text;
  }
}

This code assumes there are no existing target="" attributes. You can put this is in the default input format to force it for everyone. As an administrator you could still use Full HTML if you don't want the links to open externally. You can even provide your users with a choice by setting up a new input format like Filtered HTML, and giving them access to both the format with the NewWindow filter and the one without the filter.

--
If you have a problem, please search before posting a question.

jasonwhat’s picture

Internal links could create problems. The request seems to be for a way for users to choose by checking a box. There already is support for admin controlled link openings in the interwiki.module.

Steven’s picture

There, I updated the snippet with a simple look-ahead. It assumes internal links are always relative. Making it recognize absolute, local URLs is left as an exercise for the reader.

The only reason I made the snippet is because I'm tired of people nagging here because they want this completely annoying link-opening behaviour :P.

--
If you have a problem, please search before posting a question.

kalin_s’s picture

As I already said I can't do it myself, and I think there are enough Drupal users to benefit from you code. Thank you once again.
----------
Working on a bilingual mini-portal - www.archeologic.net

jasonwhat’s picture

while annoying, 80% of people don't know how to open links in new windows. It is a tough choice between designing for those who do know these simple tricks and the majority who don't, of course the programmers all know these things and often assume others do.

sepeck’s picture

Then education is the key, supporting a crutch is not. :)

The sad part is, I was recently asked by an IT professional (consultant who is quite good BTW), how I kept the main window of my searches open...... sigh

-sp
---------
Test site...always start with a test site.
Drupal Best Practices Guide

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

zilla’s picture

i love this feature too, but in reality it generates a lot of not-web-standards-compliant tagging and so seems to be going the way of the, uh, cellphone-without-camera...

most users are able to able launch new windows, or alternatively, consider that firefox already has a default setting for users to open all links in new windows..and ie will be including tabs as well...and so does opera

perhaps not a target=blank solution - but has anybody thought about a new way to launch a new tab as a command that relates to any link, full url or relative url? that way, it doesn't follow the url to a new location, just dupes the existing page..does that sound dumb?

kalin_s’s picture

http://extensionroom.mozdev.org/more-info/linky
----------
Working on a bilingual mini-portal - www.archeologic.net

galgoz’s picture

I agree, education of the users is the key. I added an article and some simple text on one of my sites to do just that.

Ric Fischer’s picture

I put this module on my set and when I went to administer -> modules, I received the following error:

Parse error: parse error, unexpected ']' in modules/newwindow.module on line 9

(For the picky among us, I removed the path leading up to 'modules' in that error message.)

In looking at the preg_replace, it's obvious that the code is confused about the single-quote character in the first argument.

Steven, I'm not sure what your regex (between the pair of !'s) was trying to accomplish. Would you mind cleaning it up for non-regex pro's like me? :-)

Please?

Thanks in advance.

--
Ric Fischer

dkruglyak’s picture

this module is totally broken. Is there a regexp pro here who could fix it?

Jon Williamson’s picture

Jon Williamson

I'm a new user, and totally ignorant in any type of coding.

Please accept my thanks for the help you've provided for those like myself.

lekei’s picture

One option would be to put the following on the page:

Press [Shift] to open links in a new window.

of course you could add:

<?php if(stristr($_SERVER["HTTP_USER_AGENT"],"firefox" != FALSE ) { ?>or [Ctrl] to open in a new tab.<?php } ?>

Another simple option is to add the following line:

<base target="link" >

to your template, which will make all links on the page open in a seperate window (reusing the same window). You will need to manually add "target='_self'" to all links you want to open in the current window.

To cause the default action for each link to be a new window, use
<base target="_new" >
instead.

msz3544’s picture

I am using the weblink module, but the default seems to be to open links in the same window as my site ... I cannot find where to configure it to open in a new window. I looked at this thread but when I uploaded that newwindow.module it produced the following error: warning: Cannot modify header information - headers already sent by (output started at ...). Thanks

kalin_s’s picture

I used rel="external" to modify the field_url.inc for the flexinode module so all references on my links page open in new window.
On line 43 <a href="http://'. $output .'" rel="external">.
I suppose it will have to be something similar for the weblink module.
----------
Working on a bilingual mini-portal - www.archeologic.net

msz3544’s picture

I tried adding that bit of code (rel="external") on line 43 of the flexinode field_url.inc but it didn't work. However, I added target="_blank" right before the rel="external" and that solved the problem ...

kalin_s’s picture

kalin_s’s picture

Open select links in new window
Author: Dynamic Drive
Description: This one of a kind script makes it easy to specify certain links on your page open in a new window, and with greater control than HTML's "target" attribute. You can designate arbitrary links- or even entire groups of links- open in a new window by doing one of the following:

1) Give the link a class="nwindow" attribute.
2) Or, to get an entire group of links to open in a new window, wrap them all inside a container with class="nwindowcontainer", such as

links here

.

And to make it all worthwhile, this script supports two modes of operation- automatic or manual. In the former, the designated links (by doing one of the above steps) will automatically open in a new window. In "manual" mode however, visitors use a checkbox to toggle between the designated links opening in a new window or current instead. Control and versatility is what this script is all about!

The link is http://www.dynamicdrive.com/dynamicindex8/newwindow3.htm

brooksgr’s picture

Ok... this is an admitted hack by a complete newcomer to drupal and php, but it works for right now.

I modified block.tpl.php so that if the string _blank appears in an item's description, that item will be opened in a new window. The "_blank" is stripped out of the description, so that hovering works as expected.

Here is the code:

      $thisContent = $block->content;
      $titlepos = stripos($thisContent, 'title');
      $blankflg = stripos($thisContent, '_blank');
    
 if ($blankflg > 0) {
        $thisContent = substr_replace($thisContent, '', $blankflg, 6);
        $thisContent = substr_replace($thisContent, 'target="_blank" title', $titlepos, 5);
        
        }
    
print $thisContent;
rkn-dupe’s picture

I've found this page which is a JS script to do new links in a new window... works great for me:

http://muzso.hu/node/59

LIQUID VISUAL’s picture

When people open a new link from my site, I want to keep my site open in the background. What a pig, eh? So, in the includes directory, about line 1090 in common.inc, I replaced

return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';

with

if(substr($path,0,4)=="http"){
  return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) . 'target=\"_blank\">'. ($html ? $text : check_plain($text)) .'</a>';
}
else{
  return '<a href="'. check_url(url($path, $query, $fragment, $absolute)) .'"'. drupal_attributes($attributes) .'>'. ($html ? $text : check_plain($text)) .'</a>';
}

When "http" are the first four characters in the menu item URI, the link is automatically external.

No problems so far. (5 minutes!!)

Chris Brown

Check out the Mooney's Bay Webcam, at http://liquidvisual.ca

dmordred’s picture

And what if I want to get only the rss feeds links (displayed in sidebar as block) to open in new windows?
I tried all those options but aparently they not fit in this case in particular (at least not working here tried the module and the common.inc correction)

I have very little experience, but I come from a succesfully modded php-nuke, and I was able to sometimes patch up the programming for my needs, but drupal is too abstract for me so far :-/

akifkardas’s picture

Try to change this codes in aggregator.module, it worked nice for me.

          $teaser .= '<p><a href="'. check_url($item->link) .'">'. t('read more') ."</a></p>\n";
to
          $teaser .= '<p><a href="'. check_url($item->link) .'" target="_blank">'. t('read more') ."</a></p>\n";



  $output .= '<a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";
to
  $output .= '<a href="'. check_url($item->link) .'" target="_blank">'. check_plain($item->title) ."</a>\n";



  $output = '<a href="'. check_url($item->link) .'">'. check_plain($item->title) .'</a> <span class="age">'. t('%age old', array('%age' => format_interval(time() - $item->timestamp))) .'</span>';
to
  $output = '<a href="'. check_url($item->link) .'" target="_blank">'. check_plain($item->title) .'</a> <span class="age">'. t('%age old', array('%age' => format_interval(time() - $item->timestamp))) .'</span>';



  $output .= '<h3 class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></h3>\n";
to
  $output .= '<h3 class="feed-item-title"><a href="'. check_url($item->link) .'" target="_blank">'. check_plain($item->title) ."</a></h3>\n";

Also common.inc worked nice. thanks. And I am using urlfilter.module for url and mail filtering. Anyway.. is common.inc hack only for menu? Forums and nodes keep open pages in same window again.

Mehmet Akif KARDAŞ
http://www.akifkardas.com

behring’s picture

The changes suggested by Mehmet works fine. Drupal 5.0 will incorporate an option to do this from the administration page?

Learning to use Drupal! Thank you Everyone

ozlady’s picture

Just wanted to post and say thanks to princeofwales aka Chris Brown (August 5, 2006 - 23:05) - for your code solution in common.inc.

I'm pretty new to PHP but I must say it's impressed me, and the contribution of people like yourselves is just amazing in using applications like Drupal.

Thanks again!

vinoth.3v’s picture

$(function() {
$('a[@href^=http]').not('[@href*=yourdomain.com]')
.addClass('external-link')
.click(function() {
window.open(this.href, '_blank');
return false;
});
});

In above code replace yourdomain.com with your website domain.

i am using curvy corners, so I included this script simply in that. Works :)

Vinoth - வினோத்

Vinoth - வினோத்

mcrittenden’s picture

For anyone wanting this for Drupal 6, the External New Tab module (http://drupal.org/project/external) does it.

- Mike, from Little Blue Labs and Drupal Check