Created
October 7, 2009 16:26
-
-
Save cowboy/204177 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
// Potentially part of the jQuery urlInternal plugin | |
(function($){ | |
// Method: jQuery.isUrlFragment | |
// | |
// Test whether or not a URL is a fragment. The URL can either begin with # | |
// or be a partial URL or full URI, that when navigated to, only changes the | |
// document.location.hash. | |
// | |
// Usage: | |
// | |
// > jQuery.isUrlFragment( url ); | |
// | |
// Arguments: | |
// | |
// url - (String) a URL to test the fragment-ness of. | |
// | |
// Returns: | |
// | |
// (Boolean) true if the URL is a fragment, false otherwise. | |
$.isUrlFragment = function( url ) { | |
var loc = document.location, | |
loc_fragbase = loc.href.replace( /#.*$/, '' ) + '#', | |
loc_hostbase = loc_fragbase.replace( /^(https?:\/\/.*?\/).*$/, '$1' ), | |
url_fragbase = url.replace( /#.*$/, '' ) + '#'; | |
// url is just a fragment | |
return url.indexOf( '#' ) === 0 | |
// url is absolute | |
|| url.indexOf( loc_fragbase ) === 0 | |
// url is relative, beginning with '/' | |
|| url.indexOf( '/' ) === 0 && loc_hostbase + url_fragbase.slice( 1 ) === loc_fragbase | |
// url is relative, but doesn't begin with '/' (this is very inefficient, but easy) | |
|| $('<a href="' + url + '"/>')[0].href.indexOf( loc_fragbase ) === 0; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment