The Shifted Librarian - Shifting Libraries at the speed of byte

How to Add a Script to Open Links in a New Window

Written for Radio 8 using the Transmitter template, although you should be able to adapt it to other templates as well. You should also be able to add almost any Javascript code using this same process. Send me your comments and suggestions for improving it!


In order to provide an option for visitors to choose to open links in new windows, I added some Javascript code I stole from RandomWalks. To  do this, go to your Prefs in Radio and then select your Home Page Template. Copy the following code, and paste it somewhere in between the <head> and </head> tags in the template.

<script language="JavaScript"><!--
// thanks to randomwalks.com for this code
function targetLinks(boNew) {
if (boNew)
where = "_blank";
else
where = "_self";
for (var i=0; i<=(document.links.length-1); i++) {
document.links[i].target = where;
}
}
// -->
</script>

Here's where I put it in my template.

image of code

Then scroll down further, past the <body> tag but before the </body> tag, and find where you want to put the box and text that will be visible to the user. Copy the following text, and paste it in to your template.

<form name="targeter">
<input type="checkbox" name="targetbox" id="tcheck"
    onclick="targetLinks(this.checked);">
<label for="tcheck" style="cursor: hand;">
    links open windows
</label>
</form>

This is what it will look like on your page:

This is where I put it in my template.

image of code

To change the text next to the box, simply change the text "links open windows" in the above code to whatever you want it to say. Then just submit your changes, and you're done. Wait a few seconds for the changes to upstream, and then go to your home page and test it out!

Did I miss something? Let me know.