jurlp.js | |
JQuery URL parser. | JQuery URL parser plugin for parsing, manipulating, filtering and monitoring URLs in href and src attributes within arbitrary elements (including document.location.href), as well as creating anchor elements from URLs found in HTML/text. |
Authors | Thomas James Bonner (tom.b@gmai l.com). onner |
Version | 1.0.4 |
License | Copyright © 2012, Thomas James Bonner (tom.b@gmai l.com). onner |
URL overview. | A quick quide to URL nomenclature in this plugin. |
URL Objects | URL object definition. |
Quick overview | Useful example code. |
Parsing document. | Parsing the document URL. |
Parsing elements with an “href” or “src” attribute. | Parsing “href” or “src” attributes. |
Parsing element text/ | Parsing text/HTML for URLs. |
Parsing URL strings directly. | How to directly parse, modify or monitor an arbitrary URL string. |
Unknown URLs. | Overview of unknown URL parsing. |
Operators. | Overview of filter operators. |
this parameter. | Where “this” is refered to as an argument to the method functions, it may be one of the following; |
Internal functions. | All internal private functions. |
Functions | |
initialiseElement | Initialise an element for use with the URL parser. |
initialiseElementText | Initialise an elements text field for use with the URL parser. |
setAttrUrl | Given an element, and an attribute, set the attribute to the supplied URL, and created a backup of the original URL if not already done. |
restoreAttrUrl | Given an element, and an attribute, then restore the URL attribute value to its original value. |
restoreElement | Destroys any data associated with an element that has previously been initialised for use with the URL parser, and restores the elements “href” or “src” attribute (if any) to its original value. |
getHref | Get the href URL for the element. |
updateHref | Update a segment of the elements href URL. |
updateHrefShim | Shim function for reorganising parameters before calling updateHref(). |
setHref | Sets the href URL value for an element. |
urlToObject | Parse a URL into segments using the DOM. |
objectToUrl | Convert a URL object to a string. |
sanitiseUrl | Sanitise a URL. |
urlObjectToString | Converts a URL object to a string (used to override toString for URL objects). |
setUrlSegment | Set the value of a segment within a URL string. |
getUrlObject | Convert a URL string to an object, if not already. |
getFragmentString | Retrieve the fragment string for a given URL. |
getQueryString | Retrieve the query string for a given URL. |
updateQuery | Update the query string for the elements URL. |
queryStringToObject | Convert a query string to an object. |
getQueryObject | Retrieve the query object for a given URL. |
queryObjectToString | Query objects toString method. |
getPathString | Retrieve the path string for a given URL. |
getPathObject | Retrieve the path object for a given URL. |
updatePath | Update the path string for the elements URL. |
pathObjectToString | Path objects toString method. |
getPortString | Retrieve the port string for a given URL. |
getHostString | Retrieve the host string for a given URL. |
getPasswordString | Retrieve the password string for a given URL. |
getUserString | Retrieve the user string for a given URL. |
getSchemeString | Retrieve the scheme string for a given URL. |
addSelectorCallback | Add a function the selector callstack. |
returnEachElement | Apply the callback for each element in this. |
returnEachObject | Apply the callback for each element in this, and buffer return codes. |
dispatchGetSetHelper | Dispatch to get or set helper functions depending on the arguments supplied. |
methodDispatcher | Main method dispatcher for the public interface. |
URI parser interface. | All URI parsing is handled through this interface. |
Functions | |
uri. | Converts a URI object with an “http” scheme to a string. |
uri. | Converts a URI object with a “mailto:” scheme to a string. |
uri. | Converts a URI object with a “javascript:” scheme to a string. |
uri. | Converts an generic URI object to a string. |
uri. | Parse a URI with a “http://” scheme into a URI object. |
uri. | Parse a URI with a “mailto:” scheme into a URI object. |
uri. | Parse a URI with a “javascript:” scheme into a URI object. |
uri. | Parses any URI (URIs with a scheme seperator of “://” are parsed as “http://”, everything else is treated as unknown.. |
uri. | Parse a URI string based on scheme. |
Helper interface. | All private helper methods. |
Functions | |
getUrl | Return the elements URL (stored under its “data-href”, and/or “href”/”src” attribute). |
setUrl | Set the elements URL (stored under it’s “data-href”, and/or “href”/”src” attribute). |
parseUrl | Return the URL object for the elements “data-href” attribute value. |
getFragment | Get the fragment object from the elements URL. |
setFragment | Set the fragment string for the elements URL. |
getQuery | Get the query object from the elements URL. |
setQuery | Set the query string for the elements URL. |
getPath | Get the path object from the elements URL. |
setPath | Set the path string for the elements URL. |
getPort | Get the port string from the elements URL. |
setPort | Set the port string for the elements URL. |
getHost | Get the host string from the elements URL. |
setHost | Set the host string for the elements URL. |
getPassword | Get the password string from the elements URL. |
setPassword | Set the password string for the elements URL. |
getUser | Get the user string from the elements URL. |
setUser | Set the user string for the elements URL. |
getScheme | Get the scheme string from the elements URL. |
setScheme | Set the scheme string for the elements URL. |
= (equals) | Test if the actual value is equal to the user supplied value. |
!= (not equals) | Test if the actual value is equal to the user supplied value. |
< (less than) | Test if the actual value is less than the user supplied value. |
> (greater than) | Test if the actualValue is greater than the user supplied value. |
<= (less than or equal to) | Test if the actual value is less than or equal to the user supplied value. |
>= (greater than or equal to) | Test if the actual value is greater than or equal to the user supplied value. |
*= (contains) | Test if the actual value contains the user supplied value. |
^= (starts with) | Test if the start of the actual value matches the user supplied value. |
$= (ends with) | Test if the end of the actual value is the same as the user supplied value. |
regex (regular expression) | Test if the actual value matches the user supplied regular expression. |
Public interface. | All public methods exposed via the JQuery URL parser plugin interface. |
Functions | |
url | Get/Set the href string for the given element(s). |
fragment | Get/Set the fragment segment of the URL for the given element(s). |
query | Get/Set the query segment of the URL for the given element(s). |
path | Get/Set the path segment of the URL for the given element(s). |
port | Get/Set the port segment of the URL for the given element(s). |
host | Get/Set the host segment of the URL for the given element(s). |
password | Get/Set the password segment of the URL for the given element(s). |
user | Get/Set the user segment of the URL for the given element(s). |
scheme | Get/Set the scheme segment of the URL for the given element(s). |
initialise | Initialise the parser for the given element(s). |
restore | Removes any parser data associated with the element(s), and sets the href attribute to its original value. |
goto | Set document.location.href to the supplied elements “href”, “src” or “data-href” attribute value. |
proxy | Proxy the URL. |
watch | Automatically apply all modifications to new elements added to the DOM that match the selector for the supplied elements. |
unwatch | Removes a watch previously created with watch, and prevents modifications being made to new elemenets of the same selector. |
filter | Filters elements by URL or URL segment. |
interface | Get the available methods for the parser interface. |
JQuery plugin interface. | |
Functions | |
$.fn. | Public interface/method dispatcher for the JQuery URL parser. |
$.jurlp | Returns an interface for directly parsing, manipulating and monitoring the supplied URL. |
JQuery URL parser plugin for parsing, manipulating, filtering and monitoring URLs in href and src attributes within arbitrary elements (including document.location.href), as well as creating anchor elements from URLs found in HTML/text.
Authors | Thomas James Bonner (tom.b@gmai l.com). onner |
Version | 1.0.4 |
License | Copyright © 2012, Thomas James Bonner (tom.b@gmai l.com). onner |
Thomas James Bonner (tom.b@gmai l.com). onner
Yonas Sandbæk (sel@gmai l.com). tar
Copyright © 2012, Thomas James Bonner (tom.b@gmai l.com). onner
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
A quick quide to URL nomenclature in this plugin.
Throughout this plugin, URLs are segmented and refered to in the following manner;
http://username:password@www.example.com:443/path/file.name?query=string#anchor |_____||______| |______| |_____________| |_||_____________||___________||_____| | | | | | | | | scheme user password host port path query fragment |______________________________________________________________________________| | url
Contains the protocol identifier (i.e. “https://”, “ftp://”).
Conains the username to use when connecting to the host server. This segment may be empty.
Contains the password to use in conjunction with the username when connecting to the remote server. This segment may be empty (and cannot be set without a user name).
Contains the name or IP address of the host server (i.e. “www.example.com”, or “127.0.0.1”).
Contains the listening port number for the host server (i.e. “80”, or “8080”). Note that an empty port value implies the default port (80).
Contains the file path (i.e. “/index.html”, or “/”).
Contains any parameters passed in the query (i.e. “?param1=value1¶m2=value2”). This segment may be empty.
Contains any anchors/hash tags (i.e. “#elementname”). This segment may be empty.
URL object definition.
For the purposes of this plugin, URLs can be represented either as a string, for example “http://www.example.com:8080
{ scheme: "http://" user: "username", password: "password", host: "www.example.com", port: "8080", path: "/path/file.name", query: "?query=string", fragment: "#anchor" }
Therefore, wherever URLs are supplied as a parameter to the plugin via the url or proxy methods, either a string or object representation or the URL may be supplied.
URL objects that have been returned via the parser interface can easily be converted to a string by calling the objects toString() method.
// Parse the document.location.href URL, and convert it back to a string again. $(document).jurlp("url").toString();
Useful example code.
// Parse and set the element(s) URL $("a").jurlp("url"); $("a").jurlp("url", "http://www.example.com/"); // Get or set individual URL segments for the element(s) $("a").jurlp("scheme"); $("a").jurlp("scheme", "https://"); $("a").jurlp("user"); $("a").jurlp("user", "username"); $("a").jurlp("password"); $("a").jurlp("password", "password"); $("a").jurlp("host"); $("a").jurlp("host", "www.example.com"); $("a").jurlp("port"); $("a").jurlp("port", "8080"); $("a").jurlp("path"); $("a").jurlp("path", "../file.name"); $("a").jurlp("query"); $("a").jurlp("query", {"param":"value"}); $("a").jurlp("fragment"); $("a").jurlp("fragment", "elementid"); // Filter on URL segments $("a").jurlp("filter", "scheme", "^=", "http") .jurlp("filter", "user", "=", "user") .jurlp("filter", "password", "=", "password") .jurlp("filter", "host", "=", "www.example.com") .jurlp("filter", "port", "!=", "8080") .jurlp("filter", "path", "$=", ".html") .jurlp("filter", "query", "*=", "param=value") .jurlp("filter", "fragment", "regex", /(\#)/); // Watch a selector for new nodes $("a:eq(0)").jurlp("watch", function(element, selector){}) .jurlp("filter", "host", "=", "www.example.com") .jurlp("query",{"found":"example"}); $("body").prepend("<a href='http://www.example.com/'></a>"); $("a:eq(0)").jurlp("unwatch"); // Parse an element's text for URLs and create/return anchor elements $("<div>www.example.com</div>").jurlp(); // Get an interface for parsing/manipulating the supplied URL url = $.jurlp("http://www.example.com:80/path/file.name?param1=value1#fragment"); // Parse the URL to an object. url.url(); // Get the URL scheme. url.scheme(); // Get the URL user name. url.user(); // Get the URL password. url.password(); // Get the URL host. url.host(); // Get the URL port. url.port(); // Get the URL path. url.path(); // Get the URL query. url.query(); // Get a specific parameter value from the URL query. url.query().param1; // Get the URL fragment. url.fragment(); // Set the full URL. url.url("http://www.example.com:80/path/file.name?param1=value1#fragment"); // Set the URL scheme. url.scheme("https://"); // Set the URL user name. url.user("user"); // Set the URL password. url.password("password"); // Set the URL host. url.host("www.newexample.com"); // Set the URL port. url.port("80"); // Set the URL path. url.path("/newpath/newfile.file"); // Append to the URL path. url.path("./newfile.file"); // Remove two path elements and append to the URL path. url.path("../../newfile.file"); // Set the URL query. url.query("?param=value"); // Append/modify the URL query (string or object) url.query("param=value"); url.query({"param":"value"}); // Remove the URL query url.query(""); url.query({}); // Set the URL fragment. url.fragment("#newfragment");
Parsing the document URL.
The document URL (document.location.href) can be parsed by specifying the HTML document element to the parser in the following manner;
// Parse the document.location.href URL string into a URL object $(document).jurlp("url");
Similarly, the document URL can be modified by the plugin, but it is worth noting that changes will not be directly applied to document.location.href until goto is explicitly called on the element, and instead, a working copy of the URL is stored under the documents “data-href” attribute.
// Does not modify document.location.href (updates $(document).data("href")) $(document).jurlp("url", "www.example.com"); // Does modify document.location.href (from $(document).data("href")) $(document).jurlp("goto");
Parsing “href” or “src” attributes.
Elements with an “href” or “src” attribute (i.e. <a href=””>, <base href=””>, <link href=””>, <img src=””>, <script src=””> or <iframe src=””>), can be parsed by specifying the element(s) to the parser in the following manner;
// Parse all anchor element URLs into an array $("a").jurlp("url");
Any modifications made to the URL will modify the relevant “href” or “src” attribute directly. If you want to visit the URL within an elements “href” or “src” attribute, it is possible to call goto on the element.
// Directly set the first anchor elements URL, and then goto it! $("a:eq(0)").jurlp("url", "www.example.com").jurlp("goto");
Parsing text/HTML for URLs.
It is possible for the URL parser to find URLs within text/HTML, and convert them into HTML anchor elements.
// Parse the HTML for URLs, and convert all URLs found in the text to anchors. $("<div>Here are URLs: www.example1.com, www.example2.com</div>").jurlp(); // HTML becomes: <div> Here are URLs: <a href="http://www.example1.com/" class="jurlp-no-watch">www.example1.com</a>, <a href="http://www.example2.com/" class="jurlp-no-watch">www.example2.com</a> </div>
How to directly parse, modify or monitor an arbitrary URL string.
// Get an interface for parsing the document URL... var url = $.jurlp(); // .. or get an interface for parsing your own URL. url = $.jurlp("www.example.com"); // Parse the URL to an object. url.url(); // Get the URL scheme. url.scheme(); // Get the URL host. url.host(); // Get the URL port. url.port(); // Get the URL path. url.path(); // Get the URL query. url.query(); // Get a specific parameter value from the URL query. url.query().parameter; // Get the URL fragment. url.fragment(); // Create a watch for new URLs that contain "example.com" in the host name var watch = $.jurlp("example.com").watch(function(element, selector){ console.log("Found example.com URL!", element, selector); }); // We can even apply filters to the watch to be sure! watch.jurlp("filter", "host", "*=", "example.com"); // Append a new URL, which will trigger the watch $("body").append("<a href=\"www.example.com\"></a>"); // Stop watching for "example.com" URLs. watch.jurlp("unwatch");
Overview of unknown URL parsing.
The parser will attempt to parse any type of URL it encounters based on its scheme. However, not all URLs are parsable, for example “spotify:track:<trackid>”. In this case, the following URL object is returned;
{ scheme: "spotify:", url: "track:<trackid>" }
The unknown URL object will always contain the scheme (if present), for filtering purposes, and also contains a toString() method, which will convert the URL object back to the original URL string.
”mailto:” URLs are parsable in the same manner as a regular HTTP URL. For example, the following URL object is returned for a URL with a “mailto:” scheme;
{ scheme: "mailto:" user: "username", password: "", host: "www.example.com", port: "", path: "", query: "?subject=subject&body=body", fragment: "" }
Therefore, “mailto:” URLs can be fully parsed using this parser, but note that it is not possible to set the password, port or fragment strings on a “mailto:” URL.
”javascript” URLs are parsable in the same manner as a regular HTTP URL. For example, the following URL object is returned for a URL with a “javasrcipt:” scheme;
{ scheme: "javascript:" user: "", password: "", host: "www.example.com", port: "", path: "/", query: "", fragment: "", javascript: "alert('!');" }
Therefore, “javascript:” URLs can be fully parsed using this parser, but note that the current “document.location.href” will always be parsed/returned as the main URL object.
Overview of filter operators.
The following filter operators may be specified as the “operator” parameter to the filter method.
”=” | Equal to. |
”!=” | Not equal to. |
”*=” | Contains. |
”<” | Less than. |
”<=” | Less than or equal to. |
”>” | Greater than. |
”>=” | Greater than or equal to. |
”^=” | Starts with. |
”$=” | Ends with. |
”regex” | Regular expression. |
All internal private functions.
This section contains all internal functions that perform the grunt work for the parser interface.
Functions | |
initialiseElement | Initialise an element for use with the URL parser. |
initialiseElementText | Initialise an elements text field for use with the URL parser. |
setAttrUrl | Given an element, and an attribute, set the attribute to the supplied URL, and created a backup of the original URL if not already done. |
restoreAttrUrl | Given an element, and an attribute, then restore the URL attribute value to its original value. |
restoreElement | Destroys any data associated with an element that has previously been initialised for use with the URL parser, and restores the elements “href” or “src” attribute (if any) to its original value. |
getHref | Get the href URL for the element. |
updateHref | Update a segment of the elements href URL. |
updateHrefShim | Shim function for reorganising parameters before calling updateHref(). |
setHref | Sets the href URL value for an element. |
urlToObject | Parse a URL into segments using the DOM. |
objectToUrl | Convert a URL object to a string. |
sanitiseUrl | Sanitise a URL. |
urlObjectToString | Converts a URL object to a string (used to override toString for URL objects). |
setUrlSegment | Set the value of a segment within a URL string. |
getUrlObject | Convert a URL string to an object, if not already. |
getFragmentString | Retrieve the fragment string for a given URL. |
getQueryString | Retrieve the query string for a given URL. |
updateQuery | Update the query string for the elements URL. |
queryStringToObject | Convert a query string to an object. |
getQueryObject | Retrieve the query object for a given URL. |
queryObjectToString | Query objects toString method. |
getPathString | Retrieve the path string for a given URL. |
getPathObject | Retrieve the path object for a given URL. |
updatePath | Update the path string for the elements URL. |
pathObjectToString | Path objects toString method. |
getPortString | Retrieve the port string for a given URL. |
getHostString | Retrieve the host string for a given URL. |
getPasswordString | Retrieve the password string for a given URL. |
getUserString | Retrieve the user string for a given URL. |
getSchemeString | Retrieve the scheme string for a given URL. |
addSelectorCallback | Add a function the selector callstack. |
returnEachElement | Apply the callback for each element in this. |
returnEachObject | Apply the callback for each element in this, and buffer return codes. |
dispatchGetSetHelper | Dispatch to get or set helper functions depending on the arguments supplied. |
methodDispatcher | Main method dispatcher for the public interface. |
var initialiseElement = function ( )
Initialise an element for use with the URL parser.
this | The element to initialise. See this parameter. |
var setAttrUrl = function ( attr, url )
Given an element, and an attribute, set the attribute to the supplied URL, and created a backup of the original URL if not already done.
Note, if the attribute doesn’t exist, then it will not be created.
this | The element to set the attribute URL on. |
attr | The name of the attribute to set. |
url | The value of the attributes URL. |
var urlToObject = function ( url )
Parse a URL into segments using the DOM. Parses authority information from the URL using parseUri (http://blog.stevenlevithan.com/archives/parseuri).
url | URL String to parse. |
URL object.
var setUrlSegment = function ( url, segment, value )
Set the value of a segment within a URL string.
url | The URL to modify. |
segment | The segment of the URL to modify (“scheme”, “host”, “port”, “path”, “query” or “fragment”). |
value | The new segment value. |
The URL string containing the update segment.
var returnEachElement = function ( callback, parameters )
Apply the callback for each element in this. Used for methods that return elements.
this Array of elements to iterate through.
callback Function to call for each element found.
parameters Callback function parameters (array).
Array of elements.
var returnEachObject = function ( callback, parameters )
Apply the callback for each element in this, and buffer return codes. Used for methods that return data.
this Array of elements to iterate through.
callback Function to call for each element found.
parameters Callback function parameters (array).
Array of return codes.
var dispatchGetSetHelper = function ( getHelper, setHelper, helperArguments )
Dispatch to get or set helper functions depending on the arguments supplied.
If no user arguments are supplied, perform the get, ortherwise perform the set with the user arguments.
getHelper | Get URL data callback. |
setHelper | Set URL data callback. |
helperArguments | User arguments supplied to the public interface. |
get/setHelper() return code.
All URI parsing is handled through this interface.
This section contains all parser interfaces utilised by the public parser interface.
ToDo: Extend this interface with the current URL segment parsing logic, and implement a more comprehensive URI parser set.
See http://en.wikipedia.org/wiki/URI_scheme for an overview of URIs.
Functions | |
uri. | Converts a URI object with an “http” scheme to a string. |
uri. | Converts a URI object with a “mailto:” scheme to a string. |
uri. | Converts a URI object with a “javascript:” scheme to a string. |
uri. | Converts an generic URI object to a string. |
uri. | Parse a URI with a “http://” scheme into a URI object. |
uri. | Parse a URI with a “mailto:” scheme into a URI object. |
uri. | Parse a URI with a “javascript:” scheme into a URI object. |
uri. | Parses any URI (URIs with a scheme seperator of “://” are parsed as “http://”, everything else is treated as unknown.. |
uri. | Parse a URI string based on scheme. |
Parse a URI with a “http://” scheme into a URI object.
Parses any URI (URIs with a scheme seperator of “://” are parsed as “http://”, everything else is treated as unknown..
All private helper methods.
This section contains all get/set and filter methods utilised by the public interface.
Functions | |
getUrl | Return the elements URL (stored under its “data-href”, and/or “href”/”src” attribute). |
setUrl | Set the elements URL (stored under it’s “data-href”, and/or “href”/”src” attribute). |
parseUrl | Return the URL object for the elements “data-href” attribute value. |
getFragment | Get the fragment object from the elements URL. |
setFragment | Set the fragment string for the elements URL. |
getQuery | Get the query object from the elements URL. |
setQuery | Set the query string for the elements URL. |
getPath | Get the path object from the elements URL. |
setPath | Set the path string for the elements URL. |
getPort | Get the port string from the elements URL. |
setPort | Set the port string for the elements URL. |
getHost | Get the host string from the elements URL. |
setHost | Set the host string for the elements URL. |
getPassword | Get the password string from the elements URL. |
setPassword | Set the password string for the elements URL. |
getUser | Get the user string from the elements URL. |
setUser | Set the user string for the elements URL. |
getScheme | Get the scheme string from the elements URL. |
setScheme | Set the scheme string for the elements URL. |
= (equals) | Test if the actual value is equal to the user supplied value. |
!= (not equals) | Test if the actual value is equal to the user supplied value. |
< (less than) | Test if the actual value is less than the user supplied value. |
> (greater than) | Test if the actualValue is greater than the user supplied value. |
<= (less than or equal to) | Test if the actual value is less than or equal to the user supplied value. |
>= (greater than or equal to) | Test if the actual value is greater than or equal to the user supplied value. |
*= (contains) | Test if the actual value contains the user supplied value. |
^= (starts with) | Test if the start of the actual value matches the user supplied value. |
$= (ends with) | Test if the end of the actual value is the same as the user supplied value. |
regex (regular expression) | Test if the actual value matches the user supplied regular expression. |
">" : function ( actualValue, userValue )
Test if the actualValue is greater than the user supplied value.
actualValue | Actual value. |
userValue | User supplied value. |
true | The actual value is greater than the user supplied value. |
false | The actual value is less than or equal to the user supplied value. |
Test if the actual value is less than or equal to the user supplied value.
actualValue | Actual value. |
userValue | User supplied value. |
true | The actual value is less than or equal to the user supplied value. |
false | The actual value is greater than the user supplied value. |
">=" : function ( actualValue, userValue )
Test if the actual value is greater than or equal to the user supplied value.
actualValue | Actual value. |
userValue | User supplied value. |
true | The actual value is greater than or equal to the user supplied value. |
false | The actual value is less than the user supplied value. |
"^=" : function ( actualValue, userValue )
Test if the start of the actual value matches the user supplied value.
actualValue | Actual value. |
userValue | User supplied value. |
true | The start of the actual value matches the user supplied value. |
false | The start of the actual value does not match the user supplied value. |
"$=" : function ( actualValue, userValue )
Test if the end of the actual value is the same as the user supplied value.
actualValue | Actual value. |
userValue | User supplied value. |
true | The end of the actual value matches the user supplied value. |
false | The end of the actual value does not match the user supplied value. |
All public methods exposed via the JQuery URL parser plugin interface.
Functions | |
url | Get/Set the href string for the given element(s). |
fragment | Get/Set the fragment segment of the URL for the given element(s). |
query | Get/Set the query segment of the URL for the given element(s). |
path | Get/Set the path segment of the URL for the given element(s). |
port | Get/Set the port segment of the URL for the given element(s). |
host | Get/Set the host segment of the URL for the given element(s). |
password | Get/Set the password segment of the URL for the given element(s). |
user | Get/Set the user segment of the URL for the given element(s). |
scheme | Get/Set the scheme segment of the URL for the given element(s). |
initialise | Initialise the parser for the given element(s). |
restore | Removes any parser data associated with the element(s), and sets the href attribute to its original value. |
goto | Set document.location.href to the supplied elements “href”, “src” or “data-href” attribute value. |
proxy | Proxy the URL. |
watch | Automatically apply all modifications to new elements added to the DOM that match the selector for the supplied elements. |
unwatch | Removes a watch previously created with watch, and prevents modifications being made to new elemenets of the same selector. |
filter | Filters elements by URL or URL segment. |
interface | Get the available methods for the parser interface. |
"url" : function ( url )
Get/Set the href string for the given element(s).
this | See this parameter. |
url | If present, specifies the new URL object/string to set. Otherwise the function will get the URL. |
If a URL was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of element URLs.
// Parse the document.location.href URL $(document).jurlp("url"); // Parse all URLs in anchor tags $("a").jurlp("url"); // Update the working URL for the document $(document).jurlp("url", "http://www.google.com"); // Replace all anchor tags with the google URL! $("a").jurlp("url", "http://www.google.com"); // Output the documents URL object console.log($(document).jurlp("url")); // Output the documents URL string console.log($(document).jurlp("url").toString());
"fragment" : function ( fragment )
Get/Set the fragment segment of the URL for the given element(s).
this | See this parameter. |
fragment | If present, specifies the new fragment string to set. Otherwise the function will get the fragment string from each elements URL. |
If a fragment string was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of URL fragments from each element.
// Parse the document.location.href URL for the fragment string $(document).jurlp("fragment"); // Parse all URLs in anchor tags and retrieve their fragment strings $("a").jurlp("fragment"); // Set a new fragment for the document $(document).jurlp("fragment", "elementid"); // Replace the fragment string in all anchor tags with the new element ID $("a").jurlp("fragment", "elementid"); // Output the documents URL fragment console.log($(document).jurlp("fragment"));
"query" : function ( query )
Get/Set the query segment of the URL for the given element(s).
this | See this parameter. |
query | If present, specifies the new query object to set. Otherwise the function will get the query object from each elements URL. |
If a query object was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of URL query objects from each element. Each returned query object can be converted to a string by calling its toString() method.
// Parse the document.location.href URL for the query object $(document).jurlp("query"); // Parse all URLs in anchor tags and retrieve their query object $("a").jurlp("query"); // Set/update the "new" parameter in the query string for the document $(document).jurlp("query", {"new":"parameter"}); // Remove the query string for the document $(document).jurlp("query", {}); // Update the query string in all anchor tags with the new query object. $("a").jurlp("query", {"new":"parameter"}); // Remove the query string in all anchor tags. $("a").jurlp("query", {}); // Output the documents URL query object console.log($(document).jurlp("query")); // Output the documents URL query string console.log($(document).jurlp("query").toString());
"path" : function ( path )
Get/Set the path segment of the URL for the given element(s).
this | See this parameter. |
path | If present, specifies the new path to set. Otherwise the function will get the path object from each elements URL. |
If a path was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of URL path objects from each element. Each returned path object can be converted to a string by calling its toString() method.
// Parse the document.location.href URL for the path object $(document).jurlp("path"); // Parse all URLs in anchor tags and retrieve their path object $("a").jurlp("path"); // Set a new path for the document $(document).jurlp("path", "/index.html"); // Append a path to the document URLs path $(document).jurlp("path", "./file.name"); // Append a path to the document URLs path, which removes 2 existing path // elements before appending the new path $(document).jurlp("path", "../../folder/file.name"); // Update the file name segment of the path in all anchor tags // with the new file name. $("a").jurlp("path", "../file.name"); // Remove the path in all anchor tags. $("a").jurlp("path", "/"); // Output the documents URL path object console.log($(document).jurlp("path")); // Output the documents URL path string console.log($(document).jurlp("path").toString());
"port" : function ( port )
Get/Set the port segment of the URL for the given element(s).
this | See this parameter. |
port | If present, specifies the new port to set. Otherwise the function will get the port string from each elements URL. |
If a port was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of port strings from each elements URL.
// Parse the document.location.href URL for the port $(document).jurlp("port"); // Parse all URLs in anchor tags and retrieve their port $("a").jurlp("port"); // Set a new port for the document $(document).jurlp("port", "8080"); // Replace the port in all anchor tags with the new port number $("a").jurlp("port", "8080"); // Output the documents URL port console.log($(document).jurlp("port"));
"host" : function ( host )
Get/Set the host segment of the URL for the given element(s).
this | See this parameter. |
host | If present, specifies the new host name to set. Otherwise the function will get the host name string from each elements URL. |
If a host name was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of host name strings from each elements URL.
// Parse the document.location.href URL for the host name $(document).jurlp("host"); // Parse all URLs in anchor tags and retrieve their host name $("a").jurlp("host"); // Set a new host name for the document $(document).jurlp("host", "www.example.com"); // Replace the host name in all anchor tags with the new host name $("a").jurlp("host", "www.example.com"); // Output the documents URL host name console.log($(document).jurlp("host"));
"password" : function ( password )
Get/Set the password segment of the URL for the given element(s).
Note! A password cannot be set on a URL unless a user name has been set first (see user).
this | See this parameter. |
password | If present, specifies the new password to set. Otherwise the function will get the password string from each elements URL. |
If a password was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of password strings from each elements URL.
// Parse all URLs in anchor tags and retrieve their password $("a").jurlp("password"); // Replace the password in all anchor tags with the new password string $("a").jurlp("password", "newpassword");
"user" : function ( user )
Get/Set the user segment of the URL for the given element(s).
this | See this parameter. |
user | If present, specifies the new username to set. Otherwise the function will get the username string from each elements URL. |
If a username was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of username strings from each elements URL.
// Parse all URLs in anchor tags and retrieve their username $("a").jurlp("user"); // Replace the username in all anchor tags with the new username string $("a").jurlp("username", "newusername");
"scheme" : function ( scheme )
Get/Set the scheme segment of the URL for the given element(s).
this | See this parameter. |
scheme | If present, specifies the new scheme. Otherwise the function will get the scheme string from each elements URL. |
If a scheme string was specified, then this function returns the array of modified elements for chaining purposes, otherwise it returns an array of scheme strings from each elements URL.
// Parse the document.location.href URL for the scheme $(document).jurlp("scheme"); // Parse all URLs in anchor tags and retrieve their scheme $("a").jurlp("scheme"); // Set a new scheme name for the document $(document).jurlp("scheme", "https://"); // Replace the scheme in all anchor tags href attributes $("a").jurlp("scheme", "https://"); // Output the documents URL host name console.log($(document).jurlp("scheme"));
"initialise" : function ( )
Initialise the parser for the given element(s). HTML anchor elements or the HTML document element need not be explicitly initialised.
Elements are initialised as follows;
$(document) | Initialise the “data-href” attribute for the document with the value of “document.location.href”. The “data-href” attribute will be modified instead of “document.location.href” when modifying this element. See Parsing document.location.href. |
Elements with “href”/”src” attributes | An attribute named “data-original-href” or “data-original-src” is created to store a copy of the elements original “href”/”src” attribute at the time of initialisation. See <Parsing elements with an href or src attribute>. |
All other elements | Parses the element HTML for URLs, wraps any URLs found in an anchor tag, and returns all anchor elements. |
this | See this parameter. |
Array of initialised elements (minus the parent container element).
// Not necessary $(document).jurlp(); // Not necessary $("a").jurlp(); // Parse the HTML for URLs, and convert all URLs found in the // text to anchors tags, and return the anchor elements. $("<div>www.example.com</div>").jurlp();
"restore" : function ( )
Removes any parser data associated with the element(s), and sets the href attribute to its original value.
$(document) | Removes the “data-href” attribute. |
Elements with “href”/”src” | Restores the “href”/”src” attribute to the “data-original-href/src” attribute value, and removes any other added attributes. |
All other elements | Currently there is no way to restore an elements HTML which has been converted by the parser, so consider saving it first if needed! |
this | See this parameter. |
Array of elements which were restored for chaining purposes.
// Restore the working URL for the document. $(document).jurlp("restore"); // Restore the URL for all anchor elements. $("a").jurlp("restore");
"goto" : function ( )
Set document.location.href to the supplied elements “href”, “src” or “data-href” attribute value.
this | See this parameter. |
// Goto the documents URL. $(document).jurlp("goto");
"proxy" : function ( url, parameter )
Proxy the URL. The elements URL will be replaced with the proxy URL, and the original URL will be encapsulated under the query string using the parameter name specified.
this | See this parameter. |
url | The proxy URL. |
parameter | The name of the query string parameter to encapsulate the original URL in. |
Array of modified elements for chaining purposes.
// Proxy all URLs in anchor tags to www.example.com // URL becomes "http://www.example.com/?url=<original URL>" $("a").jurlp("proxy", "http://www.example.com", "url");
"watch" : function ( callback )
Automatically apply all modifications to new elements added to the DOM that match the selector for the supplied elements. This allows URL filters/modifications that have been applied to existing elements to be propogated to new elements if the page content is being modified (i.e. inserting new anchor tags via AJAX).
Watch will monitor the selector of the supplied elements via a DOM node listener to detect when new elements are inserted. For each new element that is inserted, any prior filters or modifications made to URLs with the same selector will be applied, and the watcher will be alerted via a callback.
Note! It is not possible to call watch more than once for the same selector. To do this, try naming the selector differently, i.e. instead of “a”, use “a:not(uniqueid)”, where “uniqueid” is a nice lengthy descriptive name!
To stop watching for updates on a selecter, use unwatch.
this | Array of elements to obtain the selector from. See this parameter. |
callback | Function to call when elements are found, which is supplied two arguments, the new element that was inserted into the DOM, and the selector that triggered the watch. |
Array of unmodified elements for chaining purposes.
// Add a watch on the first anchor element, and if the host name is // "www.example.com", set the URL query string to "found=example". // The "filter" and "query" calls will also be applied to all new elements that // watch discovers!.. $("a:eq(0)").jurlp("watch", function(element, selector){ // If we get here, the first anchor element has changed to a URL containing // "www.example.com" and now contains "found=example" in the query string. // Dump the URL object to prove it! console.log($(element).jurlp("url")); }).jurlp("filter", "host", "=", "www.example.com") .jurlp("query",{"found":"example"}); // Prepend a new anchor tag to the page. This will trigger the watch on the // "a:eq(0)" selector, which will apply all prior calls to this selector, // so in this instance: // - First perform the filter host, to ensure the host name is "www.example.com". // - If the host name matches, update the URL query string with "found=example"). // If the host name does not match, then the query string will not be set. $("body").prepend ( "<a href='http://www.example.com/'></a>" ); // Stop watching for updates on the "a:eq(0)" selector. // The "a:eq(0)" selector can now be watched on again. $("a:eq(0)").jurlp("unwatch");
// As an arbitrary example, we want to modify the query string on all existing // facebook/twitter URLs, and then watch the "a" selector for all new // facebook/twitter URLs that appear, and apply the new query string to those too: // THIS WILL NOT WORK!: $("a").jurlp("watch"). .jurlp("filter", "host", "=", "www.facebook.com"). .jurlp("query",{"found":"facebook"}); // This call will fail, as the "a" selector is now watched. $("a").jurlp("watch"). .jurlp("filter", "host", "=", "www.twitter.com") .jurlp("query",{"found":"twitter"}); // THIS WILL WORK!: $("a:not(facebook)").jurlp("watch"). .jurlp("filter", "host", "=", "www.facebook.com") .jurlp("query",{"found":"facebook"}); $("a:not(twitter)").jurlp("watch") .jurlp("filter", "host", "=", "www.twitter.com") .jurlp("query",{"found":"twitter"});
"unwatch" : function ( )
Removes a watch previously created with watch, and prevents modifications being made to new elemenets of the same selector. This will also clear the list of modifications for the selector, and the selector is free to use in a sebsequent call to watch.
this | Array of elements to obtain the selector from. |
Array of unmodified elements for chaining purposes.
// Create a watch on the "a" selector $("a").jurlp("watch"); // Remove the watch on the "a" selector $("a").jurlp("unwatch");
"filter" : function ( segment, operator, value )
Filters elements by URL or URL segment.
this | Array of elements to filter. See this parameter. |
segment | The URL segment to filter on (either “scheme”, “host”, “port”, “path”, “query” or “fragment”), or “url” to filter on the full URL. See URL overview for more information. |
operator | The type of filtering to apply (either “!=”, “$=”, “*=”, “<”, “<=”, “=”, “>”, “>=” or “^=”). See Operators for more information. |
value | The value of the item to filter on. |
Filtered element array for chaining purposes.
// Get the URL object for all anchors that match // the document URLs host name. $("a").jurlp("filter", "host", "=", $(document).jurlp("host")) .jurlp("url"); // Get the URL object for all anchors that match // the document URLs host, and has a path ending in ".php". $("a").jurlp("filter", "host", "=", $(document).jurlp("host")) .jurlp("filter", "path", "$=", ".php") .jurlp("url"); // Get the URL object for all anchors whose query // string matches the regular expression $("a").jurlp("filter", "query", "regex", /(\?)/).jurlp("url");
Functions | |
$.fn. | Public interface/method dispatcher for the JQuery URL parser. |
$.jurlp | Returns an interface for directly parsing, manipulating and monitoring the supplied URL. |
$.fn.jurlp = function ( method )
Public interface/method dispatcher for the JQuery URL parser.
See Public interface for more information on the available methods.
See initialise for more specific information on how elements are initialised by the parser.
this | Element(s) to process. See this parameter. |
method | See Public interface for an overview of available methods and arguments. |
arguments | Method arguments. |
Either an array of elements for chaining purposes, or array of specific values, depending on the method called.
$.jurlp = function ( url )
Returns an interface for directly parsing, manipulating and monitoring the supplied URL.
url | The URL string to provide a URL parser interface for. Defaults to document.location.href if no URL is supplied. |
The URL parser interface for the given URL.
href | The URL string. |
url | See url. |
scheme | See scheme. |
user | See user. |
password | See password. |
host | See host. |
port | See port. |
path | See path. |
query | See query. |
fragment | See fragment. |
proxy | See proxy. |
goto | See goto. |
watch | Sets a watch for all “href” and “src” attributes containing the URLs hostname (selector is “[href*=”host”],[src*=”host”]” where host is this.host()), and returns all elements of the same selector for chaining purposes. See watch for more information. |
unwatch | Removes a watch created for the current URLs hostname. See unwatch. |
Initialise an element for use with the URL parser.
var initialiseElement = function ( )
Initialise an elements text field for use with the URL parser.
var initialiseElementText = function ( )
Given an element, and an attribute, set the attribute to the supplied URL, and created a backup of the original URL if not already done.
var setAttrUrl = function ( attr, url )
Given an element, and an attribute, then restore the URL attribute value to its original value.
var restoreAttrUrl = function ( attr )
Destroys any data associated with an element that has previously been initialised for use with the URL parser, and restores the elements “href” or “src” attribute (if any) to its original value.
var restoreElement = function ( )
Get the href URL for the element.
var getHref = function ( )
Update a segment of the elements href URL.
var updateHref = function ( segment, value )
Shim function for reorganising parameters before calling updateHref().
var updateHrefShim = function ( parameters )
Sets the href URL value for an element.
var setHref = function ( url )
Parse a URL into segments using the DOM.
var urlToObject = function ( url )
Convert a URL object to a string.
var objectToUrl = function ( url )
Sanitise a URL.
var sanitiseUrl = function ( url )
Converts a URL object to a string (used to override toString for URL objects).
var urlObjectToString = function ( )
Set the value of a segment within a URL string.
var setUrlSegment = function ( url, segment, value )
Convert a URL string to an object, if not already.
var getUrlObject = function ( url )
Retrieve the fragment string for a given URL.
var getFragmentString = function ( url )
Retrieve the query string for a given URL.
var getQueryString = function ( url )
Update the query string for the elements URL.
var updateQuery = function ( query )
Convert a query string to an object.
var queryStringToObject = function ( query )
Retrieve the query object for a given URL.
var getQueryObject = function ( url )
Query objects toString method.
var queryObjectToString = function ( )
Retrieve the path string for a given URL.
var getPathString = function ( url )
Retrieve the path object for a given URL.
var getPathObject = function ( url )
Update the path string for the elements URL.
var updatePath = function ( path )
Path objects toString method.
var pathObjectToString = function ( )
Retrieve the port string for a given URL.
var getPortString = function ( url )
Retrieve the host string for a given URL.
var getHostString = function ( url )
Retrieve the password string for a given URL.
var getPasswordString = function ( url )
Retrieve the user string for a given URL.
var getUserString = function ( url )
Retrieve the scheme string for a given URL.
var getSchemeString = function ( url )
Add a function the selector callstack.
var addSelectorCallback = function ( element, callback, parameters )
Apply the callback for each element in this.
var returnEachElement = function ( callback, parameters )
Apply the callback for each element in this, and buffer return codes.
var returnEachObject = function ( callback, parameters )
Dispatch to get or set helper functions depending on the arguments supplied.
var dispatchGetSetHelper = function ( getHelper, setHelper, helperArguments )
Main method dispatcher for the public interface.
var methodDispatcher = function ( method )
Return the elements URL (stored under its “data-href”, and/or “href”/”src” attribute).
"getUrl" : function ( )
Set the elements URL (stored under it’s “data-href”, and/or “href”/”src” attribute).
"setUrl" : function ( url )
Return the URL object for the elements “data-href” attribute value.
"parseUrl" : function ( )
Get the fragment object from the elements URL.
"getFragment" : function ( )
Set the fragment string for the elements URL.
"setFragment" : function ( fragment )
Get the query object from the elements URL.
"getQuery" : function ( )
Set the query string for the elements URL.
"setQuery" : function ( query )
Get the path object from the elements URL.
"getPath" : function ( )
Set the path string for the elements URL.
"setPath" : function ( path )
Get the port string from the elements URL.
"getPort" : function ( )
Set the port string for the elements URL.
"setPort" : function ( port )
Get the host string from the elements URL.
"getHost" : function ( )
Set the host string for the elements URL.
"setHost" : function ( host )
Get the password string from the elements URL.
"getPassword" : function ( )
Set the password string for the elements URL.
"setPassword" : function ( password )
Get the user string from the elements URL.
"getUser" : function ( )
Set the user string for the elements URL.
"setUser" : function ( user )
Get the scheme string from the elements URL.
"getScheme" : function ( )
Set the scheme string for the elements URL.
"setScheme" : function ( scheme )
Test if the actual value is equal to the user supplied value.
"=" : function ( actualValue, userValue )
Test if the actual value is equal to the user supplied value.
"!=" : function ( actualValue, userValue )
Test if the actualValue is greater than the user supplied value.
">" : function ( actualValue, userValue )
Test if the actual value is greater than or equal to the user supplied value.
">=" : function ( actualValue, userValue )
Test if the actual value contains the user supplied value.
"*=" : function ( actualValue, userValue )
Test if the start of the actual value matches the user supplied value.
"^=" : function ( actualValue, userValue )
Test if the end of the actual value is the same as the user supplied value.
"$=" : function ( actualValue, userValue )
Test if the actual value matches the user supplied regular expression.
"regex" : function ( actualValue, userValue )
Get/Set the href string for the given element(s).
"url" : function ( url )
Get/Set the fragment segment of the URL for the given element(s).
"fragment" : function ( fragment )
Get/Set the query segment of the URL for the given element(s).
"query" : function ( query )
Get/Set the path segment of the URL for the given element(s).
"path" : function ( path )
Get/Set the port segment of the URL for the given element(s).
"port" : function ( port )
Get/Set the host segment of the URL for the given element(s).
"host" : function ( host )
Get/Set the password segment of the URL for the given element(s).
"password" : function ( password )
Get/Set the user segment of the URL for the given element(s).
"user" : function ( user )
Get/Set the scheme segment of the URL for the given element(s).
"scheme" : function ( scheme )
Initialise the parser for the given element(s).
"initialise" : function ( )
Removes any parser data associated with the element(s), and sets the href attribute to its original value.
"restore" : function ( )
Set document.location.href to the supplied elements “href”, “src” or “data-href” attribute value.
"goto" : function ( )
Proxy the URL.
"proxy" : function ( url, parameter )
Automatically apply all modifications to new elements added to the DOM that match the selector for the supplied elements.
"watch" : function ( callback )
Removes a watch previously created with watch, and prevents modifications being made to new elemenets of the same selector.
"unwatch" : function ( )
Filters elements by URL or URL segment.
"filter" : function ( segment, operator, value )
Get the available methods for the parser interface.
"interface" : function ( )
Public interface/method dispatcher for the JQuery URL parser.
$.fn.jurlp = function ( method )
Returns an interface for directly parsing, manipulating and monitoring the supplied URL.
$.jurlp = function ( url )