Get URL Parts using MooTools

  • Avinash
  • 1
  • May 14, 2012
  • Web Development

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

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"
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"

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 :)

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: