Posted By

johnloy on 06/27/08


Tagged

javascript textmate utilities


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SpinZ
joomla


Javascript function to get a url parameter


 / Published in: Other
 

  1. /**
  2. * Returns url param value
  3. * @author Arash Yalpani
  4. *
  5. * @param url The url to be queried
  6. * @param paramName The params name
  7. * @return paramName's value or false if param does not exist or is empty
  8. *
  9. * @example getUrlParam('http://localhost/?a=123', 'a') => 123
  10. * @example getUrlParam('http://localhost/?a=123', 'b') => false
  11. * @example getUrlParam('http://localhost/?a=', 'a') => false
  12. */
  13.  
  14. getUrlParam: function(url, paramName) {
  15. var urlSplit = url.split('?');
  16. if (!urlSplit[1]) { // no query
  17. return false;
  18. }
  19.  
  20. var urlQuery = urlSplit[1];
  21. var paramsSplit = urlSplit[1].split('&');
  22. for (var i = 0; i < paramsSplit.length; i++) {
  23. paramSplit = paramsSplit[i].split('=');
  24. if (paramSplit[0] == paramName) {
  25. return paramSplit[1] ? paramSplit[1] : false;
  26. }
  27. }
  28.  
  29. return false;
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: NeoBlack on June 27, 2008

Hi, I have an other version of this function:

getParam: function(paramname) { var SELFLOCATION = self.location.href; var paramstring = (SELFLOCATION.indexOf('?') != -1)?(SELFLOCATION.substr(SELFLOCATION.indexOf('?')+1,SELF_LOCATION.length)):(''); var re = new RegExp('^'+paramname+'='); var params = paramstring.split('&'); for (var param in params) { if (re.test(params[param])) { return (params[param].split('='))[1]; } } return ''; }

You need to login to post a comment.