Recommend this page to a friend! |
Classes of Christian Vigh | Javascript URL Parser | README.md | Download |
|
|
DownloadINTRODUCTION$.uri is a jQuery component that allows for parsing uris. It provides several methods for retrieving query parameters, anchor value, optional username/password, port, protocol and so on. It even allows an anchor value ("#somevalue") to be interpreted as a query string. HOW TO USE IT ?This is really simple ; first, include jQuery :
Then include the uri source :
After that, your window object is populated with a new member, url ; the following example displays the query parameters for the current page :
but you can also use this component to parse any uri you want :
SPECIFIC FEATURES$.uri introduces a few extra goodies for manipulating urls. This includes :
REFERENCEThis section gives a reference of all the methods available in the $.uri object. I would have liked to propose most of them as properties but, unfortunately, support for such an incredible, new and technological breakthrough feature is not yet consistently supported amongst the tiny cast of browsers and standard makers. So you'll have to use it the jQuery way, which is not so bad indeed ; if you want to retrieve a value, just say :
and if you want to set it, just say :
All the examples in this section are based on the folllowing example url :
Examples also assume that the query parameter local_parameter is local to the url. This can be achieved by some code like :
Example values are always written in italics and enclosed in parentheses most of the time. anchor ( [value] )Gets/sets the optional anchor (#anchor). anchorParameters ( )Allows anchors to hold parameters of the form "param1=value1¶m2=value2..." , which are parsed the same way as query parameters. This trick can be useful for more or less "full ajax" pages, to add a new url to the history without reloading everything. This method accepts the same parameters as the parameters() method. credentials ( user, password )Gets/sets the user credentials for an url. Returns an object containing two members : user and password. directory ( [value] )Gets/sets the directory part after the hostname (path/subdir). The resulting property will include a leading and trailing slash, even if it was missing in the supplied value. host ( [value] )Gets/sets the hostname (www.example.com). href ( [value] )Gets/sets the whole uri (http://user:password@www.example.com:80/path/subdir/test.html?param=value¶m2=value2#anchor). If a value is specified, then all the individual properties will be assigned with the various parts of the uri value. The returned value does not include local parameters. hrefLocal ( )Returns the whole href value, including the parameters declared to be local (see locals()).
Using our example, this will return :
hrefGlobal ( )Alias : returns href(). locals ( )Sometimes you need to reload your page, passing it an additional parameter, depending on user interaction. Suppose for example that your initial page is :
then the user clicks on some "display details" link and you need to reload your page with an additional parameter :
then the user clicks a "login" link which redirects to page "login.php" ; you want all the page parameters to be passed to "login.php", but not this extra parameter, "details=1", which was meant only for the "home.php" page. The locals() method is there to specify which query parameters should be considered as "local" to the current page, and not included in the value returned by the href() and query() methods. The following example redirects to the page "login.php?param=value" :
To define local parameters, either specify an array of strings :
or a string specifying space- or comma-separated values :
page ( [value] ) ##Gets/sets the requested page (test.html). parameters ( )Returns an object having the specified properties and methods :
The parameters() function can also be called as :
'action' can be the name of any function defined in the object returned when the parameters() function is called without argument. The returned value in this case will not the object, but rather the return value of the called function. password ( [value] )Gets/sets the optional password (foobar). path ( [value] )Gets/sets the 'directory' and 'page' properties. When setting a new value, a trailing slash must be specified if the last component of the path is a subdirectory, not a page (file) name. port ( [value] )Gets/sets the optional port (80). protocol ( [value] )Gets/sets the protocol part (http). query ( [value] )Gets/sets the query string. The supplied value can include an optional leading quotation mark (?param=value¶m2=value2). The returned value does not include local parameters. queryLocal ( )Returns the query string including local parameters. Using our example, this will return :
queryGlobal ( )Alias : returns query(). user ( [value] )Gets/sets the optional user (testuser). |