Skip to content

Instantly share code, notes, and snippets.

@roykolak
Created April 13, 2009 16:25
Show Gist options
  • Save roykolak/94510 to your computer and use it in GitHub Desktop.
Save roykolak/94510 to your computer and use it in GitHub Desktop.
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