Version: 1.0, Last updated: 10/7/2009
jQuery urlInternal: Easily test URL internal-, external or fragment-ness | Version: 1.0, Last updated: 10/7/2009 |
License | Copyright © 2009 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses. |
Examples | This working example, complete with fully commented code, illustrates a few ways in which this plugin can be used. |
Support and Testing | Information about what version or versions of jQuery this plugin has been tested with, what browsers it has been tested in, and where the unit tests reside (so you can test it yourself). |
Release History | |
Methods | |
Functions | |
jQuery. | Test whether or not a URL is internal. |
jQuery. | Test whether or not a URL is external. |
jQuery. | Test whether or not a URL is a fragment in the context of the current page, meaning the URL can either begin with # or be a partial URL or full URI, but when it is navigated to, only the document.location.hash will change, and the page will not reload. |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). |
Selectors | |
Selectors | |
:urlInternal | Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). |
:urlExternal | Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). |
:urlFragment | Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). |
Support methods | |
Functions | |
jQuery. | Get the internal “Default URL attribute per tag” list, or augment the list with additional tag-attribute pairs, in case the defaults are insufficient. |
jQuery. | Constructs the regular expression that matches an absolute-but-internal URL from the current page’s protocol, hostname and port, allowing for any number of optional hostnames. |
jQuery. | Set or get the regular expression that matches an absolute-but-internal URL. |
Copyright © 2009 “Cowboy” Ben Alman, Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/
This working example, complete with fully commented code, illustrates a few ways in which this plugin can be used.
http://benalman.com
Information about what version or versions of jQuery this plugin has been tested with, what browsers it has been tested in, and where the unit tests reside (so you can test it yourself).
jQuery Versions | 1.3.2 |
Browsers Tested | Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome, Opera 9.6-10. |
Unit Tests | http://benalman.com |
Functions | |
jQuery. | Test whether or not a URL is internal. |
jQuery. | Test whether or not a URL is external. |
jQuery. | Test whether or not a URL is a fragment in the context of the current page, meaning the URL can either begin with # or be a partial URL or full URI, but when it is navigated to, only the document.location.hash will change, and the page will not reload. |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). |
jQuery. | Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). |
Test whether or not a URL is internal. Non-navigating URLs (ie. #anchor, javascript:, mailto:, news:, tel:, im: or non-http/https protocol:// links) are not considered internal.
jQuery.isUrlInternal( url );
url | (String) a URL to test the internal-ness of. |
(Boolean) true if the URL is internal, false if external, or undefined if the URL is non-navigating.
Test whether or not a URL is external. Non-navigating URLs (ie. #anchor, mailto:, javascript:, or non-http/https protocol:// links) are not considered external.
jQuery.isUrlExternal( url );
url | (String) a URL to test the external-ness of. |
(Boolean) true if the URL is external, false if internal, or undefined if the URL is non-navigating.
Test whether or not a URL is a fragment in the context of the current page, meaning the URL can either begin with # or be a partial URL or full URI, but when it is navigated to, only the document.location.hash will change, and the page will not reload.
jQuery.isUrlFragment( url );
url | (String) a URL to test the fragment-ness of. |
(Boolean) true if the URL is a fragment, false otherwise.
Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). If URL cannot be determined, remove the element from the collection.
jQuery('selector').urlInternal( [ attr ] );
attr | (String) Optional name of an attribute that will contain a URL to test internal-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). If URL cannot be determined, remove the element from the collection.
jQuery('selector').urlExternal( [ attr ] );
attr | (String) Optional name of an attribute that will contain a URL to test external-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). If URL cannot be determined, remove the element from the collection.
Note that in most browsers, selecting $(“a[href^=#]”) is reliable, but this doesn’t always work in IE6/7! In order to properly test whether a URL attribute’s value is a fragment in the context of the current page, you can either make your selector a bit more complicated.. or use .urlFragment!
jQuery('selector').urlFragment( [ attr ] );
attr | (String) Optional name of an attribute that will contain a URL to test external-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Selectors | |
:urlInternal | Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). |
:urlExternal | Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). |
:urlFragment | Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). |
Filter a jQuery collection of elements, returning only elements that have an internal URL (as determined by jQuery.isUrlInternal). If URL cannot be determined, remove the element from the collection.
jQuery('selector').filter(':urlInternal'); jQuery('selector').filter(':urlInternal(attr)');
attr | (String) Optional name of an attribute that will contain a URL to test internal-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Filter a jQuery collection of elements, returning only elements that have an external URL (as determined by jQuery.isUrlExternal). If URL cannot be determined, remove the element from the collection.
jQuery('selector').filter(':urlExternal'); jQuery('selector').filter(':urlExternal(attr)');
attr | (String) Optional name of an attribute that will contain a URL to test external-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Filter a jQuery collection of elements, returning only elements that have an fragment URL (as determined by jQuery.isUrlFragment). If URL cannot be determined, remove the element from the collection.
Note that in most browsers, selecting $(“a[href^=#]”) is reliable, but this doesn’t always work in IE6/7! In order to properly test whether a URL attribute’s value is a fragment in the context of the current page, you can either make your selector a bit more complicated.. or use :urlFragment!
jQuery('selector').filter(':urlFragment'); jQuery('selector').filter(':urlFragment(attr)');
attr | (String) Optional name of an attribute that will contain a URL to test fragment-ness against. See jQuery.elemUrlAttr for a list of default attributes. |
(jQuery) A filtered jQuery collection of elements.
Functions | |
jQuery. | Get the internal “Default URL attribute per tag” list, or augment the list with additional tag-attribute pairs, in case the defaults are insufficient. |
jQuery. | Constructs the regular expression that matches an absolute-but-internal URL from the current page’s protocol, hostname and port, allowing for any number of optional hostnames. |
jQuery. | Set or get the regular expression that matches an absolute-but-internal URL. |
Get the internal “Default URL attribute per tag” list, or augment the list with additional tag-attribute pairs, in case the defaults are insufficient.
In the jQuery.fn.urlInternal and jQuery.fn.urlExternal methods, as well as the :urlInternal and :urlExternal selectors, this list is used to determine which attribute contains the URL to be modified, if an “attr” param is not specified.
a | href |
base | href |
iframe | src |
img | src |
input | src |
form | action |
link | href |
script | src |
jQuery.elemUrlAttr( [ tag_attr ] );
tag_attr | (Object) An object containing a list of tag names and their associated default attribute names in the format { tag: ‘attr’, ... } to be merged into the internal tag-attribute list. |
(Object) An object containing all stored tag-attribute values.
Constructs the regular expression that matches an absolute-but-internal URL from the current page’s protocol, hostname and port, allowing for any number of optional hostnames. For example, if the current page is http://benalman.com/test or http://www.benalman.com/test, specifying an argument of “www” would yield this pattern:
/^(?:http:)?\/\/(?:(?:www)\.)?benalman.com\//i
This pattern will match URLs beginning with both http://benalman.com/ and http://www.benalman.com/. If the current page is http://benalman.com/test, http://www.benalman.com/test or http://foo.benalman.com/test, specifying arguments “www”, “foo” would yield this pattern:
/^(?:http:)?\/\/(?:(?:www|foo)\.)?benalman.com\//i
This pattern will match URLs beginning with http://benalman.com/, http://www.benalman.com/ and http://foo.benalman.com/.
Not specifying any alt_hostname will disable any alt-hostname matching.
Note that the plugin is initialized by default to an alt_hostname of “www”. Should you need more control, jQuery.urlInternalRegExp may be used to completely customize the absolute-but-internal matching pattern.
jQuery.urlInternalHost( [ alt_hostname [, alt_hostname ] ... ] );
alt_hostname | (String) An optional alternate hostname to use when testing URL absolute-but-internal-ness. |
(RegExp) The absolute-but-internal pattern, as a RegExp.