# Opening links in a new window

**URL:** <https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439>\
**Category:** Redcloth\
**Created:** [August 26, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439 "2009-08-26T20:19:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 26, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/1 "2009-08-26T20:19:08Z")

</div>

Hi,

Short question:

How do I create links that open in a new browser window/tab without  
resorting to raw HTML?

Long question:

So I want to have links like so:

[This is a link](http://wherever.com)

I’ve got some javascript that runs on every page after it is loaded  
and translates the rel=‘external’ into target=‘\_blank’. Stupid XHTML  
1.0 strict doesn’t allow target=‘\_blank’ even though all browsers  
support it, so the javascript magic is purely so my html is validated  
against it.

I know I can just put raw HTML in, but I don’t want to. There has to  
be an easier way, using the lovely RedCloth syntax. Help?

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 28, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/2 "2009-08-28T13:23:38Z")

</div>

Em 26-08-2009 18:18, Asfand Yar Q. escreveu:

> Hi,
> 
> …  
> I’ve got some javascript that runs on every page after it is loaded  
> and translates the rel=‘external’ into target=‘\_blank’. Stupid XHTML  
> 1.0 strict doesn’t allow target=‘\_blank’ even though all browsers  
> support it, so the javascript magic is purely so my html is validated  
> against it.

Hi Asfand, sorry I don’t know the answer, but I’m curious. Why do you  
need it to be validated as XHTML strict?

The idea behind XHTML strict is to make your site accessible. Using  
Javascript per se makes your site innacessible (more than using  
target=“\_blank”), since Javascript is not standardized.

And what do you mean by “all browsers”? Do you know there are console  
browsers, brailer browsers, cell phones, etc? Opening a new page is not  
always clear in all environments. It is easy to understand that a knew  
page is open when a normal person stands in front of a common desktop  
browser. But XHTML stricts aims at accessibility and that is why the  
target attribute is not allowed. If you are not concerned about making  
your site accessible to blind people or mobile users, than you should  
not be using XHTML Strict, in my opinion. Choose one that allows target  
attribute instead of using Javascript where you don’t need it.

Or does your client asked the pages to be validated with XHTML Strict?  
If so, and if they are concerned about accessibility, the approach you  
are taking will not be accepted by it. But if the client just doesn’t  
know what XHTML Strict mean, then feel free to continue doing it that  
way.

Rodrigo.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 28, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/3 "2009-08-28T15:27:42Z")

</div>

In other words it’s not possible - that’s all I needed to know 🙂

If a person’s browser does not support opening links in a new window,  
then the link will simply open in the same window. In other words, by  
conforming to XHTML 1.0 Strict, the functionality is not reduced, just  
a bit of convenience goes away.

But for the majority of my users’ browsers, the bit of extra  
convenience is added to allow the link to open in a new window.

2009/8/28 Rodrigo Rosenfeld R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 29, 2009, 9:09am UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/4 "2009-08-29T09:09:34Z")

</div>

I’d say using Javascript instead of target="\_blank" is a good call.  
May I suggest, then, just using a CSS class to denote which should be  
external? Any JS library should be able to observe all A tags of a  
certain class and make them open in a new window.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 29, 2009, 11:31am UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/5 "2009-08-29T11:31:13Z")

</div>

That’s actually a brilliant idea. I didn’t think of that. It makes  
perfect sense. I was using the guidelines of some article that said  
use rel=external instead of target=\_blank.

On the other hand, having thought about what Rodrigo said, and read a  
few articles, I think I might just stop opening links in a new window  
anyway. It confuses or annoys users apparently.

Thanks all

2009/8/29 Jason G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 3, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/opening-links-in-a-new-window/174439/6 "2009-09-03T13:18:38Z")

</div>

Hi 🙂

javascript is the better option. For the user-centre view, I’m pissed  
off  
by links that make new windows on the same site. If someone jumps me to  
a  
‘side bar’, ‘note’ or ‘reference’, then I might prefer a new window if  
it  
is ‘substantial’ and a pop-up or overlay if it is a small description  
(for  
example).

What if you are not sure about someone who is NOT me? You could bring  
up an  
overlay pop-up and ask if he wants links opened in a new window or as  
overlays and record that preference.

Just a thought

Take care,  
\_w\_/

2009/8/29 Asfand Yar Q. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 2009/8/29 Jason G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I’d say using Javascript instead of target=“\_blank” is a good call. May  
> > I

aloha,  
\_w\_/
