Opening External Links with Prototype
Since my post on opening external links with jQuery has proven popular, I thought I would port that same technique to Prototype. Yes, it’s just as laughably trite of an example, but hey, it gives me something to write about.
My example will make use of the dom:loaded event, which fires once the document has finished loading, excluding images:
document.observe("dom:loaded", function() {
$$("a[rel~=external]").each(function(el) {
el.observe("click", function(event) {
window.open(this.href);
event.preventDefault();
});
});
});
Like I said above, dead simple. We’re perusing the document for anchor tags containing the rel attribute external, registering a click event to the anchor tag, opening the link in a new window, and finally, stopping the browser default action, which in this case means following the link.
It’s slightly more verbose than my jQuery example, but it gets the job done just the same.
Tags: JavaScript, Prototype
September 29th, 2008 at 9:35 pm
[...] public links >> prototype Opening External Links with Prototype First saved by todashdarkness | 2 days ago Toy Prototype!!!! First saved by tranceport | 11 [...]