1

I'm a bit of a jQuery monkey and I've had a site dropped on me where I need to use a simple function to open links with rel="external" in a separate window.

In jQuery I do it like this:

// make links with rel=external open in new window/tab
$(function() {
 $('a[rel*=external]').click( function() {
 window.open(this.href);
 return false;
 });
});

How would I set up the same function in Prototype to be triggered unobtrusively on any links with rel=external attached to them?

Thanks

Simon

EDIT:

Following the advice of all three responses below the final code is:

 $(document).on('dom:loaded', function() {
 $$('a[rel*=external]').invoke('on','click', function(event) {
 window.open(this.href);
 event.stop();
 });
});

Thanks all!

Rob W
351k87 gold badges811 silver badges683 bronze badges
asked Jan 26, 2011 at 9:39

1 Answer 1

5
Event.observe(window, 'load', function() {
 $$('a[rel*=external]').each(function(item) {
 $(item).observe('click', function(event) {
 window.open(this.href);
 event.stop();
 });
 });
});

And here's a demo.

answered Jan 26, 2011 at 9:45
Sign up to request clarification or add additional context in comments.

2 Comments

I would use "$(document).on('dom:loaded'" instead of "Event.observe(window, 'load'" and $(item).on instead of $(item).observe (it's the new 1.7 syntax)
@Stefaan, nice. I had forgotten the introduction of on. Also the .each can be reduced to .invoke('on', 'click', function(event)...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.