WebsiteBaker Logo
  • *
  • Templates
  • Help
  • Add-ons
  • Download
  • Home
*
Welcome, Guest. Please login or register.

Login with username, password and session length
 

News


WebsiteBaker 2.13.6 is now available!


Will it continue with WB? It goes on! | Geht es mit WB weiter? Es geht weiter!
https://forum.websitebaker.org/index.php/topic,32340.msg226702.html#msg226702


The forum email address board@websitebaker.org is working again
https://forum.websitebaker.org/index.php/topic,32358.0.html


R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html


* Support WebsiteBaker

Your donations will help to:

  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!

You can donate by clicking on the button below.


  • Home
  • Help
  • Search
  • Login
  • Register

  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Open external links in new window by default
  • Print
Pages: [1]   Go Down

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

Argos

  • Guest
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.
Logged

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 »
Logged

Argos

  • Guest
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!
Logged

  • Print
Pages: [1]   Go Up
  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Open external links in new window by default
 

  • SMF 2.0.19 | SMF © 2017, Simple Machines
  • XHTML
  • RSS
  • WAP2