Created
April 13, 2009 16:25
-
-
Save roykolak/94510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var JSClassObserver = Class.create({ | |
findElements: function(class_name) { | |
var self = this; | |
$('.' + class_name).each(function(index, element) { | |
if(element.js_attached == null) { | |
element.js_attached = {}; | |
} | |
if(element.js_attached[class_name] != true) { | |
self.attachBehavior(element, index); | |
element.js_attached[class_name] = true; | |
$(element).removeClass(class_name); | |
} | |
}); | |
} | |
}); | |
var LinkObserver = {}; | |
LinkObserver.attachExternalLink = Class.create(JSClassObserver.prototype, { | |
init: function() { | |
this.findElements('js_external_link'); | |
}, | |
attachBehavior: function(element) { | |
$(element).click(function(event) { | |
new_window = window.open($(this).attr('href')); | |
LinkObserver.opened_windows.push(new_window); | |
event.preventDefault(); | |
}); | |
} | |
}); | |
LinkObserver.attachAllObservers = function() { | |
new LinkObserver.attachExternalLink(); | |
} | |
// Example | |
/* | |
Before: | |
<a href="http://www.google.com" class="js_external_link">Click for new window</a> | |
After: | |
<a href="http://www.google.com">Click for new window</a> | |
<script type="text/javascript"> | |
alert($('a').js_attached.js_external_link); | |
</script> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment