Inspired by Automatic external link interstitials using JavaScript and Behavior.js (via Ajaxian), I decided to try to implement the same idea using Yahoo! User Interface Library. (No, I don't have new ideas, just sample code... read on!)
Here's the meat of the code:
<script type="text/javascript" src="js/YAHOO.js"></script>
<script type="text/javascript" src="js/event.js"></script>
<script type="text/javascript" src="js/dom.js"></script>
<script type="text/javascript">
<!-- <![CDATA[
var obl = {
outbound_url: "outbound.php?url=",
safe_domains: [
new RegExp("^https?://[^/]*(your\.org|another\.org)", "i")
],
assignEvents: function () {
var anchors = document.getElementsByTagName("a");
checkAnchors:
for (var i=0; i<anchors.length; i++) {
a = anchors[i];
url = a.href.toLowerCase();
checkSafeDomains:
for (var j=0; j<obl.safe_domains.length; j++) {
if ( url.match(obl.safe_domains[j]) ) {
continue checkAnchors;
}
}
el = YAHOO.util.Dom.get(a);
YAHOO.util.Event.addListener(el, "click", obl.callback, a);
}
},
callback: function (e, a) {
var oburl = obl.outbound_url + encodeURIComponent(a.href);
a.href=oburl;
}
}
window.onload = function () {
obl.assignEvents();
}
// ]]> -->
</script>
I did it, yeah? I provided a ZIP of the sample files involved.
In addition to the site mentioned above, Forget addEvent, use Yahoo!'s Event Utility was a big help too.
Comments, corrections, questions, and especially improvements are encouraged!
Thanks for the linkage, Patrick!
Very nice indeedy. I see you're finding the advantages of the YUI libraries. They make writing JavaScript so much easier.
How to Put Your Face Next to Your Comment
This is a blog entry by Phil.
Permalink: Mar 8, 2006
Tags: web nerdism
Previously: Take a Running Jump Here
Presently: Migrating to WordPress
There are 3 Comments
Latest by: Dustin Diaz
Add Your Comment!
Trackback Link: http://philsown.org/2006/03/outbound-link-warnings-with-yul/trackback
Phil Harrington is a thirty something man paying the bills with mad programming skillz, owning a home, being a dad, kicking butt, and growing-up more in Gulf-Coast Florida.
"The secret to creativity is knowing how to hide your sources." -- Einstein
© Copyright 1999-2007 philsown.org. All rights reserved.
Mar 9, 2006
Nice - I'll have to look into those Yahoo libraries. I'll link you in from my original post.