Outbound Link Warnings with YUL

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!

Comments

Patrick Fitzgerald Patrick Fitzgerald
Mar 9, 2006

Nice - I'll have to look into those Yahoo libraries. I'll link you in from my original post.

Phil Phil
Mar 9, 2006

Thanks for the linkage, Patrick!

Dustin Diaz Dustin Diaz
Mar 19, 2006

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

Add Your Comment to:  Outbound Link Warnings with YUL


(Required not Shown)

(Optional, use http://yoursite.com and I'll Link with 'Follow')



"The secret to creativity is knowing how to hide your sources." -- Einstein