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!
Trackback: http://philsown.org/2006/03/outbound-link-warnings-with-yul/trackback
Comments
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
Jul 4, 2008
Nice - I'll have to look into those Yahoo libraries. I'll link you in from my original post.