Most people will be trying to get values from a query string by using the .split() javascript function. Basically the way to do this is to grab your string after the first question mark
string = string.substring(string.indexOf("?")+1);
then splitting on ampersand
values = string.split("&");
then going through each value checking for the left hand side and returning the right if it matches
for(i=0;i\<values.length;i++)
if((values[i].split("="))[0] == name)
try{return (values[i].split("="))[1]}catch(e){return ""};
return "";
I have also done it in the past just using string indexOfs:
//zMSmyStr is the query string, zMStagline is the variable name being searched for
function zMSgetValue(zMSmyStr,zMStagline)
{
zMSmyStr = zMSmyStr+"=";
var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
return "";
zMStagline += "&";
return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}
but I decided to save space to redo these as a regex and save time and space. So here it is:
function fd_fn_Query(myQS)
{
try{
return myURL.match(new RegExp("\[?&]"+myQS+"=([^&]*)"))[1];
}catch(e){return "";}
}
much smaller and easier to use, feel free to take this and modify to suit your needs.
Wednesday, March 18, 2009
Subscribe to:
Post Comments (Atom)
1 comment:
I will still have to get my head around this in detail, but thank to your heritage (seriously!!!) I have got my head around regex ;) - i.e in javascript (still battling with pearl, but that may not be a regex thing at all I suspect)
awesome stuff here - keep posting!
peter
Post a Comment