Monthly Archives: November 2010

URL parameters in Javascript

I wanted a piece of code in pure javascript ( no framework required ) that could extract the parameters in the query string part of an URL.

I wanted it to be able to extract the parameters in this format name[key]=value  like they are used in php applications.

I found a piece of code on some other blogs or forum posts but it didn't work as I expected so here is my take on this.

  1.  
  2. function get_url_params(){
  3. var _GET = {};
  4. var s = location.search.replace( /^\?|#.*$/g, '' );
  5. if( s ) {
  6. var qsParts = s.split('&');
  7. var i, nv;
  8. for (i = 0; i < qsParts.length; i++) {
  9. var nv = qsParts[i].split('=');
  10. var n=decodeURIComponent(nv[0]);
  11. var v=decodeURIComponent(nv[1]);
  12. var k=n.split(/[\[\]\.]/);
  13. if(k.length){
  14. if(_GET[k[0]])_GET[k[0]][k[1]]=v;
  15. else {
  16. if(parseInt(k[1])==k[1])
  17. _GET[k[0]]=[];
  18. else _GET[k[0]]={};
  19. _GET[k[0]][k[1]]=v;
  20. }
  21. }else _GET[n] = v;
  22. }
  23. }
  24. return _GET;
  25. }
  26.  

This function one limitation: It doesn't work with multidimensional arrays. It's probably not hard to modify it to work like that but I only needed it to work with single dimension arrays.

Hidden Affiliate Links 1.0

Finally after a long time, The Hidden Affiliate Links plugin for WordPress 3.0.1 is here.

The previous version stopped working a long time ago when wordpress 2.5 was released ( yes it's really that long ago ).

I tested the current version on WordPress 3.0.1. It might work on older versions but I don't plan to support them.

The download, install and usage instructions are on the plugin's page.

I tried to host this plugin on the official wordpress plugins repository so you could see update notifications right in wordpres admin but apparently someone there doesn't like the fact that this plugin hides links so it was not accepted.

I think this is wrong because there are other plugins there that can somehow hide links ( like the redirection plugin ) and even without a plugin if someone wants to hide a link they can still do it easily by using any url shortner.

Anyway the idea is if you want to be notified about updates make sure you subscribe to my RSS feed.

Feel free to ask any questions about this plugin in the comments.