Recent Topics

How to open links in a new window

Started by on Sep 03, 2004 – Contents updated: Sep 03, 2004

Sep 03, 2004 19:17    

I'm posting this How To at the request of Isaac, who seemed to think my comments were helpful. Flattery will get you anywhere!

If you want links from your blog to open in a new browser window, you have several options.

One is to include the target="_blank" attribute to your link tag, like this:


<a href="" target="_blank">
Link to Michael Broder's blog!</a>

Another option is to include a bit of JavaScript code in your _main.php file that will open links in a new window when a user checks a box on a corresponding form.

Here's the code for the JavaScript function:


<script language="JavaScript">
function targetLinks(boNew)
   if (boNew) where = "_blank";
   else where = "_top";
   for (var i=0; i<=(document.links.length-1); i++)
   document.links[i].target = where;

And here's the HTML code for the form:


<input type="checkbox" name="targetnew"
Check if you want links to open in a new window

Like all JavaScript functions, this one can go anywhere in your _main.php file, as long as it's before the function call. However (also like all JavaScript functions), it SHOULD go in the HEAD segment.

The HTML code for the form should go where you want it to appear in your blog, which will generally be somewhere in the sidebar.

Hope this helps.


Sep 13, 2004 03:59

Thanks, Mike.

I did push Mike to put this here just to get a solution up and running, and it gave me a start on doing it another way. I don't want to come off as poopooing his method, and I [url=]even tried out something like it[/url]. But as I've thought about it more, using the target attrib is not really optimal. Also, you don't necessarily want local links to open new windows, even if you want remote links to (like on this forum.)

I posted another way to do this over at

You'd still do all the updating to your skin's _main.php file, where all the other XHTML is. Put keepYaLinksPoppin.js in your skin/skin_name folder, and then have a tag like this in the head section:


<script language="JavaScript" type="text/javascript" src="keepYaLinksPoppin.js"></script>

The advantage of Mike's system is that there's a checkbox that, when checked, makes the links behave one way, and when unchecked, they go back to normal. I haven't done that, but it shouldn't be too hard to stick it on.

Ok, after half an hour playing with it, it's trickier than I thought, or I'm just all JavaScripted out for the day. If a disableable feature is a must-have, then use Mike's method, but that's not as cross-browser.

Jul 11, 2005 01:24

Guys, I just wanted to thank you for your work on this issue. Isaac, I have been able to implement your wonderful creation on my site and it is exactly what I was looking for. I have an inline frame (no flaming for that, please), and I was getting perturbed that external links would open in that frame. It seemed incredibly tedious to go through and alter all of the links and/or expect visitors who leave comments to add "_blank" tags. So, I found this and it works perfectly. It still opens comments in the iframe, but opens the commentor's site in a new window. Basically, I'm posting to show my appreciation and to add this to anyone else who might search for inline frame solutions. Thanks again.

Form is loading...

multiblog engine – This forum is powered by b2evolution CMS, a complete engine for your website.