views:

41

answers:

2

I need a way to create an object from a location value. For example if I have the value:

'http://www.legis.state.pa.us/cfdocs/billinfo/bill_history.cfm?syear=2007&sind=0&body=*S*&type=B&bn=1'

which I get from doc.location.href();

I would like to make the following object:

myObject = {

  syear : '2007',
  snid  : '0',
  body  : 's',
  type  : 'B',
  bn    : '1',

};

Thanks!

D


Progress

For some reason I couldn't get the extended function/object to work in my system. I think because my code is running inside a firefox extension and outside of the document context. Or maybe it had something to do with the jQuery.noConflict(); (I don't know).

But, I ended up needing to take out the function from the object and editing it a little in order to get it to work for me:

function getURLParam(URL, strParamName){

  var qString;
  var returnVal = new Array();

  if (URL==null) return null;

  if ( URL.indexOf("?") > -1 ){
    var strQueryString = URL.substr(URL.indexOf("?")+1);
    qString = strQueryString.split("&");
  }
  else {
    return null;
  }

  for (var i=0;i<qString.length; i++){
    if (escape(unescape(qString[i].split("=")[0])) == strParamName){
      returnVal.push(qString[i].split("=")[1]);
    }

  }  

  if (returnVal.length==0) return null;
  else if (returnVal.length==1) return returnVal[0];
  else return returnVal;
}
+1  A: 

this might be useful:

http://www.mathias-bank.de/2006/10/28/jquery-plugin-geturlparam/

using the above you can easily construct the hash

mkoryak
This is exactly what I was looking for. And the source code is very small. Thanks!
DKinzer
A: 

jQuery Url Parser should do exactly what you want.

jQuery.url.setUrl('http://www.legis.state.pa.us/cfdocs/billinfo/bill_history.cfm?syear=2007&amp;sind=0&amp;body=S&amp;type=B&amp;bn=1').param('syear');

returns 2007

Boris Guéry