Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created October 7, 2009 16:26
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 cowboy/204177 to your computer and use it in GitHub Desktop.
Save cowboy/204177 to your computer and use it in GitHub Desktop.
// 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