Skip Navigation

Outbound Link Warnings with YUL

3 Comments | Latest by: Dustin Diaz | Add Your Comment! »»

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!

Mar 8, 2006 |

«« Previous Entry  ·  Next Entry »»

Comments

Patrick Fitzgerald Patrick Fitzgerald
Jul 24, 2008

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

Phil Phil
Jul 24, 2008

Thanks for the linkage, Patrick!

Dustin Diaz Dustin Diaz
Jul 24, 2008

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, and Linked with 'Follow')



If you can't see my mirrors, I can't see you.