It was asked how to make links, created by urlfilter.module, open it a new window. All you need to do is add the basic HTML code for opening a new window (target="_blank").

Although most recommend letting the user choose if a new window should open, this isn't a univeral truth, so here's a 'patch' below:

// simply added target="_blank"
$text = eregi_replace("([ \n\r\t])((http://|https://|ftp://|mailto:)([a-zA-Z0-9@:%_~#?&=.,/-]*[a-zA-Z0-9@:%_~#?&=/-]))([.,]?)([ \n\r\t])", '\1<a href="/" target="_blank">\2</a>\5\6', $text);

// didn't change this one since most e-mail links open software (I think)
// but you can choose to add: target="_blank" after: href="mailto:\2";
$text = eregi_replace("([ \n\r\t])([A-Za-z0-9._]+@[A-Za-z0-9._]+\.[A-Za-z]{2,4})([.,]?)([ \n\r\t])", '\1<a href="mailto:\2";>\2</a>\3\4', $text);

// simply added target="_blank"
$text = eregi_replace("([ \n\r\t])(www\.[a-zA-Z0-9@:%_~#?&=.,/-]*[a-zA-Z0-9@:%_~#\?&=/-])([.,]?)([ \n\r\t])", '\1<a href="http://\2"; target="_blank">\2</a>\3\4', $text);

--- In response to :


Project: Urlfilter

Version: 4.3.0

Component: Code

-Category:

+Category: feature requests

Priority: normal

Assigned to: Anonymous

Reported by: Anonymous

Updated by: Anonymous

Status: active

I'd like linked pages to open in their own separate browser window. How
can I modify the module's code to add target="_blank" at the end of
every URL?

Anonymous

---

CommentFileSizeAuthor
#3 urlfilter.targetblank.patch995 bytessun

Comments

beginner’s picture

Status: Closed (fixed) » Needs review

Why was this closed?
in cvs (future Drupal 4.7), this feature is not implemented, but it would be nice.

or even better: have an admin/settings/urlfilter where we can set whether links to other sites open in another window or not, and the same choice for links to within the same site... but that is asking a lot...

(I think open in another window - even for all links indescriminately - would be the default choice of many web masters).

deekayen’s picture

Title: Re: urlfilter links to open in new windows » "smartness" option to open other sites in new window and internal links not
Status: Needs review » Active

I went ahead and committed the patch, but I'll leave it open per beginner's feature comment.

AFAIK, target="_blank" for mailto: links is harmless. I tested it with no ill effects.

sun’s picture

Title: "smartness" option to open other sites in new window and internal links not » open external urls in new window
Status: Active » Needs review
StatusFileSize
new995 bytes

Attached patch is against HEAD.

I don't think that urlfilter has to distinguish between internal and external links, since I would say that authors linking to internal contents always use an anchor (with or without a RTE). What do others think about that?

beginner’s picture

What I do in my sites, is that I have a special class for links to another site.

In a way, I have either internal links:
<a href="node/123">click here</a>
or links to other sites:
<a href="http://drupal.org/node/5982" class="offsite" target="_blank">click here</a>

The extra css class helps the user to know what to expect (if they are familiar with the site ;) ).

sun’s picture

Sounds well to me. But I'd prefer to have a neat icon next to external links (as f.e. found on wikipedia).

So urlfilter would have to look in the current theme directory for an icon (f.e. link-external.png) to display next to a link. In addition urlfilter adds a CSS class to external links, f.e. class="link-external", along with the target="_blank".

Throwing that together, urlfilter needs to check whether a link contains the $base_url of the site (if set) or the current $_SERVER['HTTP_HOST'] variable (I'd prefer that to support sites running under multiple domain names). If so, urlfilter adds the above stated code to the processed link.

In either way, it has to be possible for designers, to alter the display of urls on their sites, f.e. decide whether to display the link icon left or right or put something else around all links. So the output of urlfilter has to be themable.

I think this sounds like a well approach and should be able to realize. Am I missing something?

sun’s picture

Sorry for spamming. Does anyone know if Allow overriding of links returned by modules stands in correlation with this issue here?

beginner’s picture

Sun,
thanks for the link. I am not sure if that issue applies in this case. The links we are dealing with are part of the content of a node, not system links created by modules. It's possible I'm missing something, though.

I prefer class="offsite" to link-external, since I already have this class all over my sites ;).
If you want to add support for adding a .png somewhere after the link, ok, but a picture can be added via css, too. A css class should be enough.

btw, are you aware this module has no official maintainer?
http://drupal.org/node/65116
Are you interested in the maintainership?
(I'm not: I working on other Drupal issues).

rkn-dupe’s picture

I've found this page which is a JS script to do external links in a new window and it works great:

http://muzso.hu/node/59

sun’s picture

We do not need Javascript to open external links in a new window. We can do this while generating the hyperlinks. But to do that we have to consider if we want to have external links opened in a new window at all.

aalex’s picture

Please make it an option !!

Administer >> settings >> urlfilter
Open URLs in a new window [yes]

:-)

Steven’s picture

Status: Needs review » Closed (won't fix)

Opening links in a new window is bad for usability and breaks XHTML validation. Won'tfix for the n'th time.