Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created June 24, 2010 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenharman/1b1c48ed39f679c77fc4 to your computer and use it in GitHub Desktop.
Save stevenharman/1b1c48ed39f679c77fc4 to your computer and use it in GitHub Desktop.
jQuery global events
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Global jQuery Events with namespaces???</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.4.2", {uncompressed:true})
google.load("jqueryui", "1.8.2", {uncompressed:true})
google.setOnLoadCallback(function() {
$('#messages').bind('bacon.goodStuff bacon_goodStuff', function(e, args){
$(this).empty()
.hide()
.append('handled ' + args.content + ': ' + e.type)
.slideDown('slow');
})
$('#global_namespace').click(function(e){
$.event.trigger('bacon.goodStuff', {content: 'global event'})
})
$('#global_longname').click(function(e){
$.event.trigger('bacon_goodStuff', {content: 'global event'})
})
$('#element_namespace').click(function(e){
$('#messages').trigger('bacon.goodStuff', {content: 'element event'})
})
$('#element_longname').click(function(e){
$('#messages').trigger('bacon_goodStuff', {content: 'element event'})
})
$('button').button()
})
</script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/hot-sneaks/jquery-ui.css" type="text/css" media="all" />
<style type="text/css">
.global {
margin-bottom: 1em;
}
</style>
</head>
<body>
<div id='messages' class='ui-widget ui-state-default ui-state-highlight ui-corner-all' style='display:none;'></div>
<p>
<h2>raise some events!</h2>
<div class='global'>
<button id='global_namespace' class="ui-state-error">global event: bacon.goodStuff</button>
<button id='global_longname'>global event: bacon_goodStuff</button>
</div>
<div class='element'>
<button id='element_namespace'>element event: bacon.goodStuff</button>
<button id='element_longname'>element event: bacon_goodStuff</button>
</div>
</p>
<p>why don't global events with namespaces (i.e. the pink button) get triggered?</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment