Created
March 25, 2014 01:07
-
-
Save bennadel/9753430 to your computer and use it in GitHub Desktop.
Binding Events To Non-DOM Objects With jQuery
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
<script type="text/javascript"> | |
( | |
function(){ | |
var strMessage = "Hello"; | |
alert( strMessage ); | |
} | |
)(); | |
</script> |
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
<script type="text/javascript" src="jquery-1.3.2.js"></script> | |
<script type="text/javascript"> | |
// Our plugin will be defined within an immediately | |
// executed method. | |
( | |
function( $ ){ | |
// Default to the current location. | |
var strLocation = window.location.href; | |
var strHash = window.location.hash; | |
var strPrevLocation = ""; | |
var strPrevHash = ""; | |
// This is how often we will be checkint for | |
// changes on the location. | |
var intIntervalTime = 100; | |
// This method removes the pound from the hash. | |
var fnCleanHash = function( strHash ){ | |
return( | |
strHash.substring( 1, strHash.length ) | |
); | |
} | |
// This will be the method that we use to check | |
// changes in the window location. | |
var fnCheckLocation = function(){ | |
// Check to see if the location has changed. | |
if (strLocation != window.location.href){ | |
// Store the new and previous locations. | |
strPrevLocation = strLocation; | |
strPrevHash = strHash; | |
strLocation = window.location.href; | |
strHash = window.location.hash; | |
// The location has changed. Trigger a | |
// change event on the location object, | |
// passing in the current and previous | |
// location values. | |
$( window.location ).trigger( | |
"change", | |
{ | |
currentHref: strLocation, | |
currentHash: fnCleanHash( strHash ), | |
previousHref: strPrevLocation, | |
previousHash: fnCleanHash( strPrevHash ) | |
} | |
); | |
} | |
} | |
// Set an interval to check the location changes. | |
setInterval( fnCheckLocation, intIntervalTime ); | |
} | |
)( jQuery ); | |
</script> |
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
$( window.location ).trigger( | |
"change", | |
{ | |
currentHref: strLocation, | |
currentHash: fnCleanHash( strHash ), | |
previousHref: strPrevLocation, | |
previousHash: fnCleanHash( strPrevHash ) | |
} | |
); |
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
<script type="text/javascript"> | |
// Bind a change handler to the window location. | |
$( window.location ).bind( | |
"change", | |
function( objEvent, objData ){ | |
var jLog = $( "#log" ); | |
// Add the URL change. | |
jLog.append( | |
"<li>" + | |
"Hash changed from " + | |
"<strong>" + objData.previousHash + "</strong>" + | |
" to " + | |
"<strong>" + objData.currentHash + "</strong>" + | |
"</li>" | |
); | |
} | |
); | |
</script> |
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
<body> | |
<h1> | |
jQuery Top-Level Closure Example | |
</h1> | |
<p> | |
<a href="#linkA">Link A</a> | | |
<a href="#linkB">Link B</a> | | |
<a href="#linkC">Link C</a> | |
</p> | |
<p> | |
The following is a change log of URL updates: | |
</p> | |
<ol id="log"> | |
<!--- Items to be injected. ---> | |
</ol> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment