Web Development

Get URL Parts using MooTools

Recently I was working with JavaScript(MooTools) and I need to get the different parts of any given URL in javascript variable. So I have done a quick search and found a very good class created by the MooTools team. Actually this class is a part of MooTools more library.

With this class we just need to pass the URL and we can get different part of that URL, like host, port, directory, query string, etc.

Here is the class which I am referring in this article. So have a look into code example for how to get URL parts using MooTools (Javascript).

We will check this with below test URL:

Test URL: http://user:[email protected]:8383/the/path.html?query=value#anchor

[cc lang=”javascript”]

var url = ‘http://user:[email protected]:8383/the/path.html?query=value#anchor’;
var my_uri_obj = new URI(url);

my_uri_obj.get(‘scheme’); //returns “http”,
my_uri_obj.get(‘user’); //returns “user”,
my_uri_obj.get(‘password’); //returns “password”,
my_uri_obj.get(‘host’); //returns “www.test.com”,
my_uri_obj.get(‘port’); //returns “8383”
my_uri_obj.get(‘directory’); //returns “/the/”
my_uri_obj.get(‘file’); //returns “path.html”
my_uri_obj.get(‘query’); //returns “query=value”
my_uri_obj.get(‘fragment’); //returns “anchor”
[/cc]

So this is how you can get the different part of the URL using MooTools More Class(URL). This helps me to solve my problem, I hope this will helps you in same way. Best Luck :)

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *