Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to parse parameters from a query string using this jquery plugin and this sample query string:

http://test.com/?xnJTqqWEclJnnPEvWH&cid=3DF3-00-=63-D4-DA-2F-91-6B-39-39-75-E4-C1-B7-28-12&mid=3D93-36-14-46-4D-52-9E-48-17-=6A-50-13-56-FA-0A-06&PROMO_MONTH=3D201106

Using the code for the plugin:

console.log($.getQueryParam( "cid" ));

The cid gets divided at the first instance of the "=" sign - so instead of:

3DF3-00-=63-D4-DA-2F-91-6B-39-39-75-E4-C1-B7-28-12

I get

3DF3-00-

Is this normal behavior? Does the equals sign do something in particular in this query string? If not, how do I parse this query string so that I get the entire parameter?

share|improve this question
    
Url encode the cid value. –  Kris Krause Aug 29 '11 at 20:57
1  
Yes, the equal sign is special as it separates the parameter name from the value. However, a better parser would probably detect two "consecutive" = (no & in between). Still, afaik, it has to be encoded if you want to use it in a value. –  Felix Kling Aug 29 '11 at 20:57
    
I can't change the values being passed in the query string. I can only manipulate what I receive from the URL. –  mheavers Aug 29 '11 at 21:00
    
You cannot change or encode the query string? Is there a guarantee that it will always be the last parameter? (I am thinking that you might have to string parse it, and not use the jQuery function). –  Kris Krause Aug 29 '11 at 21:04
add comment

3 Answers

up vote 2 down vote accepted

Use gup instead. I see it used in quite a few places and it works well. It is very simple. Note that I made a small change in the fiddle to shoehorn in the url. The code below is what you should actually use.

http://jsfiddle.net/mrtsherman/trqJ8/

gup('cid');

function gup( name )
{
 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]"+name+"=([^&#]*)";
 var regex = new RegExp( regexS );
 var results = regex.exec( window.location.href );
 if( results == null )
    return "";
 else
    return results[1];
}
share|improve this answer
    
nice! And no plugin required! –  mheavers Aug 29 '11 at 21:23
    
Mind you, this does not support arrays, etc. Also it should have: return decodeURIComponent(results[1]); there is no easy way to do it. I decided to use jquery.query.js. –  Ciantic Nov 26 '12 at 12:19
add comment

The plugin itself wasn't written to accommodate a string like that. The culprit is the following piece of code...

if (params[0] == param) {
 return params[1] || '';
}

Shortly before this step the plugin script does a split() on the equal sign creating an array. The above code snippet should be changed to something like the following to make your query retrieval work...

if (params[0] == param) {
    var paramOut = '';
    for (var p = 1; p < params.length; p++) {
        paramOut += params[p] + '=';
    }
    return paramOut.substr(0, paramOut.length - 1); // to remove the trailing equal sign
} else {
    return '';
}

Here is the plugin patched with my revisions in case you're still interested in using it...

(function($){$.getQueryParam=function(param){var pairs=location.search.substring(1).split('&');for(var i=0;i<pairs.length;i++){var params=pairs[i].split('=');if(params[0]==param){var paramOut='';for(var p=1;p<params.length;p++){paramOut+=params[p]+'=';}return paramOut.substr(0, paramOut.length-1);}else{return '';}}return undefined;};})(jQuery);
share|improve this answer
    
+1 for treating the condition as opposed to the symptom. You should notify the plugin author! –  mrtsherman Aug 29 '11 at 21:26
add comment

It is. "=" is used as a delimiter of parts of the query, so you should escape it. Yandex search engine does it so: http://yandex.ru/yandsearch?text=%3D

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.