simonyoung
simonyoung

Reputation: 855

Prototype rewrite of jQuery function

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!

Upvotes: 1

Views: 899

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 5

Related Questions