Recommend this page to a friend! |
Download .zip |
Info | Example | View files (5) | Download .zip | Reputation | Support forum (1) | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2015-12-17 (10 months ago) | Not enough user ratings | Total: 72 | All time: 427 This week: 27 |
Version | License | JavaScript version | Categories | |||
thrak-uri 1.0 | BSD License | 1.0 | Browser, jQuery |
Description | Author | |
This object can parse URLs and extract their components. |
$.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.
This is really simple ; first, include jQuery :
<script language='javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>
Then include the uri source :
<script language="javascript" type="text/javascript" src="thrak.uri-1.0.0.js"></script>
After that, your window object is populated with a new member, url ; the following example displays the query parameters for the current page :
alert ( "Current query parameters : " + window. url. query ( ) ) ;
but you can also use this component to parse any uri you want :
var testurl = $. uri ( 'http://testuser:foobar@www.example.com:80/path/subdir/test.html?param=value¶m2=value2#anchor' ) ;
alert ( "Query parameters : " + testurl. query ( ) ) ;
$.uri introduces a few extra goodies for manipulating urls. This includes :
This 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 :
var value = myobject. thevalue ( ) ;
and if you want to set it, just say :
myobject. thevalue ( 'this is the new value' ) ;
All the examples in this section are based on the folllowing example url :
http://testuser:foobar@www.example.com:80/path/subdir/test.html?param=value¶m2=value2&local_parameter=local_value#anchor
Examples also assume that the query parameter local_parameter is local to the url. This can be achieved by some code like :
var url = $.uri ( the_above_example_url ) ;
$url. locals ( 'local_parameter' ) ;
Example values are always written in italics and enclosed in parentheses most of the time.
Gets/sets the optional anchor (#anchor).
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.
Gets/sets the user credentials for an url.
Returns an object containing two members : user and password.
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.
Gets/sets the hostname (www.example.com).
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.
Returns the whole href value, including the parameters declared to be local (see locals()).
Using our example, this will return :
http://testuser:foobar@www.example.com:80/path/subdir/test.html?param=value¶m2=value2&local_parameter=local_value#anchor
Alias : returns href().
Sometimes you need to reload your page, passing it an additional parameter, depending on user interaction. Suppose for example that your initial page is :
home.php?param=value
then the user clicks on some "display details" link and you need to reload your page with an additional parameter :
home.php?param=value&details=1
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" :
window. location. href = "login.php" + window. url. query ( ) ;
To define local parameters, either specify an array of strings :
url. locals ( [ 'display', 'localaparam1', 'localparam2' ] ) ;
or a string specifying space- or comma-separated values :
url. locals ( 'display localparam1 localparam2' ) ;
Gets/sets the requested page (test.html).
Returns an object having the specified properties and methods :
get ( name, default\_value ) : Gets an url parameter from the query string by its name. A default value can be specified if the parameter does not exist.
If default\_value is not specified, the default value will be an empty string. If no parameter name is specified, then the whole parameters object is returned.
The parameters() function can also be called as :
parameters ( action [, arguments] ) ;
'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.
Gets/sets the optional password (foobar).
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.
Gets/sets the optional port (80).
Gets/sets the protocol part (http).
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.
Returns the query string including local parameters. Using our example, this will return :
?param=value¶m2=value2&local_parameter=local_value
Alias : returns query().
Gets/sets the optional user (testuser).
Files |
File | Role | Description |
---|---|---|
index.html | Example | Example script |
LICENSE | Lic. | License text |
NOTICE | Data | Some disclaimer information |
README.md | Doc. | Documentation |
thrak.uri-1.0.0.js | Class | Class source file |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
0% |
|
|
User Comments (1) | |||||
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.