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
---
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | urlfilter.targetblank.patch | 995 bytes | sun |
Comments
Comment #1
beginner commentedWhy 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).
Comment #2
deekayen commentedI 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.
Comment #3
sunAttached 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?
Comment #4
beginner commentedWhat 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 ;) ).
Comment #5
sunSounds 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 thetarget="_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?
Comment #6
sunSorry for spamming. Does anyone know if Allow overriding of links returned by modules stands in correlation with this issue here?
Comment #7
beginner commentedSun,
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).
Comment #8
rkn-dupe commentedI'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
Comment #9
sunWe 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.
Comment #10
aalex commentedPlease make it an option !!
Administer >> settings >> urlfilter
Open URLs in a new window [yes]
:-)
Comment #11
Steven commentedOpening links in a new window is bad for usability and breaks XHTML validation. Won'tfix for the n'th time.