Open External Links in New Window

Shows how to automatically open all external links in a new window

Since version 2, Plone has the new function scanforlinks() which scans the div with id="content" for external links. This function inserts a span tag with the CSS class link-external around the link's a tag, which causes a world-icon to be displayed before the link. The function can also set the target attribute of the a tag to open the link in a new window, but this behaviour is disabled by default. To enable it, you can customize the javascript.

In Plone 2.0.5 this function can be found in plone_javascript.js. With the introduction of the Resource Registry in the Plone 2.1.x series, Plone's javascript has been split into multiple smaller files. As a result of this, this function can now be found in mark_special_links.js.

  1. Depending on your Plone version, use the ZMI to go to either portal_skins/plone_ecmascript/plone_javascripts.js (2.0.5) or portal_skins/plone_ecmascript/mark_special_links.js (2.1.2), select one of your skin folders (e.g. custom) and Customize it.
  2. Switch to the skin folder where the copy of plone_javascripts.js lies and uncomment the following line:
       //links![i].setAttribute('target','_blank')
    

Change it to:

   links![i].setAttribute('target','_blank')

...and it will open all external links in separate windows. Useful for intranet use cases, but please don't use this approach on public web sites.

why?

Posted by w1ley at May 24, 2005 06:37 PM
Just wondering,
why would you not do this on a public site? I for one HATE that when I leave the site I am visiting when I am doing research. Having the portal open a new window is, in my opinion, a better approach.

Is there a reason other than personal preference to not enable this on a public portal?

re: why? (not make external links open in new window on public site)

Posted by Aaron Wallentine at Oct 11, 2007 08:48 PM
I believe the rationale for this is that these days, browsers allow for opening in new tabs/windows easily at the user's discretion. So forcing links to open in a new window overrides the users control over whether or not to open a link in a new window/tab.

I agree with this, if I want to open something in a new tab, I will right click->open in new tab on the link.

However, it is my experience that many clients are still insistent on this, and so I will do it anyway if they cannot be talked out of it.

Syntax

Posted by Pascal Dall'Aglio at Jun 27, 2005 03:16 PM
Hi the syntax on my plone_javascript was more like :
links[i].setAttribute('target','_blank')
It works fine.
Thanks

use _new instead of _blank for IE

Posted by dysolution at Jan 13, 2006 08:42 PM
I found that the only way to get both Firefox and IE to open links in a new window was to use this:

links[i].setAttribute('target','_new');

_new instead of _blank for IE

Posted by Aaron Wallentine at Oct 11, 2007 09:11 PM
Hmmm _blank worked for me for IE6, not sure about IE7 though, since I have no access to a "real" IE7 to try it out (only the hacked standalone from tredosoft.com)

Changes in Plone 2.1?

Posted by Alain Vachoux at Oct 08, 2005 08:11 AM
The file portal_skins/plone_ecmascript/plone_javascripts.js does no more exist in Plone 2.1 (or I'm too new to Plone/Zope to figure it out). Actually a number of how-tos and docs seem to require updates to accomodate changes in 2.1.

Moved

Posted by Knud Riishojgaard at Oct 14, 2005 11:51 AM
The function is still in plone_ecmascript, now in mark_special_links.js.

Its fully documented in the source.

Didn't Work for Me

Posted by Nancy Donnelly at Sep 12, 2006 06:29 PM
I didn't have that line of code in my file. I a line with had everything but the bang "!", so I copied and pasted it under that line (which also was commented out), then uncommented it. The drop-down boxes in my Plone disappeared! I took it out.
beno

re: Didn't Work for Me

Posted by Aaron Wallentine at Oct 11, 2007 11:19 PM
you may be using a newer version of plone ---

see comment Posted by Ronnie Stevens at May 30, 2007 - 13:04 below

Doesn't work in navigation portlet!!

Posted by Manuel Ortega at Nov 21, 2006 07:33 PM
Hi, thanks for your How-to.

I have followed your how-to but the external link (_blank) doesn't work in the navigation portlet.
I have found that your indications only work on the main frame of the page.

I have also encountered problems in external link of the drop down menu that I have made following how-to instruction in plone.org

If you could help me fix my problem i would be very gratefull.

Thanks

making it work for links outside content area (incl. navigation portlet) (and tal:attributes links)

Posted by Aaron Wallentine at Oct 11, 2007 11:10 PM
I was able to get this to work.

The issue is that the script mark_special_links.js calls a function, getContentArea, to grab the root element that it will look for and change links in. getContentArea grabs an element with id 'register-content' or failing that, 'content'. So I changed it to grab 'visual-portal-wrapper' which in my version of plone is the outermost div, right inside the body.

getContentArea is defined in portal_skins/plone_ecmascript/register_function.js

I changed it to:

function getContentArea() {
    // returns our content area element
    if (W3CDOM) {
        var node = document.getElementById('visual-portal-wrapper');
        if (!node) {
            node = document.getElementById('content');
        }
        return node;
    }
}

I also had another problem, and that was that it wasn't getting the links that I had made using a <a tal:attributes="href linkvalue" /> type of syntax in zpt -- the reason being that there was whitespace inserted in front of the "http://" and the js is using a linkval.indexOf('http://') == 0 to check for http links. So I added the following line to mark_special_links.js strip out whitespace, right after

var linkval = links[i].getAttribute('href');

linkval = linkval.replace(/\s/g, "");

Hope this helps ya.

I'm using plone 2.5.1, I know that in plone 3 this script is disabled by default because it uses css to check for external links.

Open links in external window plone 2.5.2

Posted by Ronnie Stevens at May 30, 2007 01:04 PM
In plone 2.5.2 there is documentation in the file mark_special_links.js:
 // set open_links_in_external_window in plone_javascript_variables.js.pt
 // to true if you want external links to be opened in a new
 // window.

So customizing plone_javascript_variables.js.pt is the way to go in this plone-version.

open https:// links in an external window?

Posted by Matt Newsted at Jul 05, 2007 02:24 PM
This works great for my regular http:// links, but for some reason, my https:// links continue to open in the same window. Any ideas would be much appreciated. If I'm missing something obvious, sorry to be a bother. Thanks!

https:// links

Posted by Aaron Wallentine at Oct 11, 2007 11:17 PM
If your plone version is similar to mine (plone 2.5), look in mark_special_links.js --

you might change this line

else if ( linkval.indexOf('http:') != 0 )

to

else if ( (linkval.indexOf('http:') != 0) && (linkval.indexOf('https:') != 0) )

this line tests the link is not an http link. If it fails, then it determines that it is an http link, and it goes to the next else branch and runs the link-modifying code.

migrating from 2.0.5

Posted by Paul S Hahn at Sep 16, 2007 04:31 AM
If you have migrated a site from 2.0.5 to 2.1.x and have customized plone previously, you will need to delete the custom plone_javascript.js for the changes made in mark_special_links.js to work.

Up-to-date

Posted by Dieter Zakel at Apr 20, 2008 10:04 AM
Ladies and Gentlemen:
If this item is outdated, what is up-to-date?

Plone 3.X

Posted by simon kampmann at May 30, 2008 12:38 PM
Plone 3.X has intergrated the seeting in the Plone Control Panel under Theme.
Plone 3.x: Plone site setup -> Theme