[wp-testers] target links in posts

Amit Gupta wp at igeek.info
Fri Dec 9 12:51:46 GMT 2005


Andy Skelton <skeltoac at gmail.com> wrote:
|  The option in quesion, "open link in new window," will probably not
|  remain as a way to use the target attribute. If someone would like to
|  research and suggest a 100% in-line solution, such as an onclick
|  function that doesn't have to be pre-defined, and it's valid and
|  doesn't break any browsers, we'll use that instead of simply removing
|  the option.
|
|  Anyone interested?

yes well, the other approach will be javascript dependent. the links can 
be
tagged by the 'rel' attribute and marked as 'external' and then a 
javascript
function can be called on page load to parse whole (x)HTML code to
make the links with 'rel="external"' to be opened in a new window.

I posted a working JS function in the hackers list thread
"XHTML Strict compliant replacement for target=_new" sometime back which
I post again below

~~~~~~~~~
function externalLinks() {
    if(!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for(var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if(anchor.getAttribute("href") && anchor.getAttribute("rel") == 
"external") {
            anchor.onclick = 
function(){window.open(anchor.getAttribute("href"));return false;};
        }
    }
}
window.onload = externalLinks;
~~~~~~~~~
an example link would be
~~~~~~~~~

<a href="http://google.com/" rel="external">Google</a>
~~~~~~~~~

though I think that instead of using this onload approach(above), Simon 
Willison's addLoadEvent()
function at http://simon.incutio.com/archive/2004/05/26/addLoadEvent 
would be better, so that
if any user already has something on window load event, the above JS 
function's call on
window load won't break that!!

this IMHO is a clean solution, however if you don't want to depend on a 
JS function then the
other solution can be to output the 'window.open' in the 'onclick' event 
of the link if its set
to be opened in a new window. that would create a lot of useless code(if 
there are many links)
as 'window.open' would be put on all of them. here's how an example link 
will look like

~~~~~~~~~
<a href="http://google.com/" 
onclick="javascript:window.open(this.getAttribute('href')); return 
false;">Google</a>
~~~~~~~~~

both of these work in IE6, Opera8.5 & FireFox1.5 for sure, I dunno about 
other browsers.

------------
Amit Gupta
http://igeek.info/  ||  http://blog.igeek.info/
http://blog.igeek.info/wp-plugins/igsyntax-hiliter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://comox.textdrive.com/pipermail/wp-testers/attachments/20051213/979a03eb/attachment-0001.htm


More information about the wp-testers mailing list