Javascript URL to Object

Hi Guys,

So I had this weird request from my colleague to transform url we are browsing for our website to Object.

So say for example you have a url like this one

http://tests/urltoobject.html?h=t&p=k#45

And you want every piece of information in that url available as object.

Now Javascript comes with various objects that are worth exploring, One of them is called location Object with have some really handy properties

Location Object Properties are given below

Property Description
hash Gives the anchor portion of a URL
host Gives the hostname and port of a URL
hostname Gives the hostname of a URL
href Gives the entire URL
pathname Gives the path name of a URL
port Gives the port number the server uses for a URL
protocol Gives the protocol of a URL
search Gives the query portion of a URL

 

Now that said we can easily extend the above object of just create a function that will do a little bit more, say for example If I want details for location for the url I mentioned above I get this output

Now let me introduce a function that I wrote as is given below

  1. function urlToObject() {
  2. var obj = {
  3. hash: window.location.hash,
  4. hostname: window.location.hostname,
  5. href: window.location.href,
  6. host: window.location.host,
  7. origin: window.location.origin,
  8. querystring: window.location.search.substr(1),
  9. search: window.location.search,
  10. port: window.location.port,
  11. protocol: window.location.protocol,
  12. pathname: window.location.pathname,
  13. scriptname: window.location.pathname.substr(1),
  14. queryvars_array : null,
  15. queryvars_assoc_obj : null,
  16. };
  17.  
  18. var params = {}, key;
  19. var params_arr = [];
  20. var keyvals = window.location.search.substr(window.location.search.indexOf('?') + 1).split('&');
  21.  
  22. for(var i = 0; i < keyvals.length; i++)
  23. {
  24. key = keyvals[i].split('=');
  25. params_arr.push(key[1]);
  26. params['"'+key[0]+'"'] = key[1];
  27. }
  28. obj.queryvars_assoc_obj = params;
  29. obj.queryvars_array = params_arr;
  30. return obj;
  31. }
  32.  
  33. console.log(urlToObject());

It is sort of extending the Location object and adding few more properties like queryvars_array or queryvars_assoc_obj so that you can access query string key=val as obj.key

output of the above code is shown below. You can reduce the above code a bit. I have done property by property map so that you know what’s happening. I’ll show you how to extend location object using jQuery in a sec.

Advertisement

 

 

Al right now as you can see what really has happened above. Now lets explore jQuery Alternative, the best part of the method below is that we are extending the actual location object thus functions under location object will be available from the new object

  1. var locObj = function(){
  2. var loc = window.location;
  3. loc = jQuery.extend({
  4. queryvars_array : null,
  5. queryvars_assoc_obj : null,
  6. scriptname: window.location.pathname.substr(1),
  7. querystring: window.location.search.substr(1)
  8. },loc);
  9. var params = {}, key;
  10. var params_arr = [];
  11. var keyvals = window.location.search.substr(window.location.search.indexOf('?') + 1).split('&');
  12. for(var i = 0; i < keyvals.length; i++) {
  13. key = keyvals[i].split('=');
  14. params_arr.push(key[1]);
  15. params['"'+key[0]+'"'] = key[1];
  16. }
  17. loc.queryvars_assoc_obj = params;
  18. loc.queryvars_array = params_arr;
  19. return loc;
  20. }
  21. console.log(locObj());

The above method will return pretty much what you saw in the above screenshot of my javascript console in chrome browser

I hope this helps

Cheers

Advertisement

 

 

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Comments

  1. thanx for your blog

Speak Your Mind

*

CommentLuv badge