Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2012, 09:55:49 PM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155528 Posts in 21713 Topics by 7737 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Open external links in new window by default  (Read 893 times)
Argos
Moderator
**
Offline Offline

Posts: 2160


WWW
« 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:
<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:
//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

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
maverik

Offline Offline

Posts: 1568



WWW
« 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:
$("a").filter(function() {
    return this.hostname && this.hostname !== location.hostname;
}).addClass('external').attr("target", "_blank");

and css

Code:
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
« Last Edit: November 16, 2009, 01:57:19 AM by Argos » Logged

Signatur wird geladen...
Argos
Moderator
**
Offline Offline

Posts: 2160


WWW
« Reply #2 on: November 16, 2009, 02:09:15 AM »

Hi Maverick, that's a cool alternative, thanks!
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!