Author Topic: Open external links in new window by default  (Read 3039 times)

Offline Argos

  • Posts: 2542
  • Gender: Male
    • Argos Media
Open external links in new window by default
« on: November 15, 2009, 11:58:52 PM »
In all my sites I use a little javascript that automatically makes sure that all links to other domains open in a new window. it overrides local link settings, so setting target="_blank" etc is not required anymore. It's a very handy script, and I think it would be nice to add it to the core settings of WB if possible, or make it into something that other uses can easliy add to their installation. I'm not sure about the best way. This is the call you need to add to the HEAD of your template:
Code: [Select]
<script language="JavaScript1.2" type="text/JavaScript" src="<?php echo TEMPLATE_DIR?>/linkstarget.js"></script>
Of course the path can be different, the script can be placed anywhere, as long as you point to it.

The script itself is:
Code: [Select]
//1)Enter domains to be EXCLUDED from opening in new window:
var excludedomains=["yourdomain.com", "www.yourdomain.com"]

//2)Automatically open offsite links in new window? (1=yes, 0 will render a checkbox for manual selection)
var auto=1

var excludedomains=excludedomains.join("|")
rexcludedomains=new RegExp(excludedomains, "i")

if (!auto)
document.write('<form name="targetmain"><input type="checkbox" name="targetnew" checked onClick="dynamiclink()">Open off-site links in new window</form>')

function dynamiclink(){

if (auto||(!auto&&document.targetmain.targetnew.checked)){
for (i=0; i<=(document.links.length-1); i++) {
if (document.links[i].hostname.search(rexcludedomains)==-1&&document.links[i].href.indexOf("http:")!=-1)
document.links[i].target="_blank"
}
}
else
for (i=0; i<=(document.links.length-1); i++) {
if (document.links[i].hostname.indexOf(mydomain)==-1)
document.links[i].target=""
}
}

if (auto)
window.onload=dynamiclink

Where "yourdomain.com" in the top of the script should be replaced by the domain you use. Maybe a WB coder can rework it into something more WB'ish, or give tips on how to do that myself? I'm not a coder, and my php and javascript skills are limited.
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

Offline maverik

  • Posts: 1572
  • Gender: Male
  • ..:: viva los tioz ::..
Re: Open external links in new window by default
« Reply #1 on: November 16, 2009, 12:27:31 AM »
this what you do will jquery do with the filter function. and with an extra css class you can put some nice icons to the link.

if  jquery is loadet in the head then use

Code: [Select]
$("a").filter(function() {
    return this.hostname && this.hostname !== location.hostname;
}).addClass('external').attr("target", "_blank");

and css

Code: [Select]
a.external {
    background: url(/path/to/your image/external.png) center right no-repeat;
    padding-right: 13px;
}

so no other script then jquery is nessesary.

it works on www.WebsiteBaker.ne t

sorry for my bad english , i hope you understand what i mean

greets


[gelöscht durch Administrator]
« Last Edit: November 16, 2009, 01:57:19 AM by Argos »

Offline Argos

  • Posts: 2542
  • Gender: Male
    • Argos Media
Re: Open external links in new window by default
« Reply #2 on: November 16, 2009, 02:09:15 AM »
Hi Maverick, that's a cool alternative, thanks!
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

 

postern-length