tags:

views:

49

answers:

3

Hi,

I am storing a string (url) in a variable it can look like one/two/three/test.aspx?ID=12312 I want to extract the querystring value, not from window.location but from the html

I thought I could use substring and write

var link = ($(this).find('a').attr('href')); 
var qs = link.substring( link.indexOf("="),  text.indexOf(" ") )

but it returns the string before the = and not after.

What am I doing wrong? Thanks in advance.

A: 

Just omit the second parameter. In this case, should return the end of the string starting at the provided index.

Fyodor Soikin
Hi, I tried that before but the script break. Tried again and it works so must have been something elsevar linkID = link.substring( link.indexOf("=")+1 ) did it.
Peter
Glad this helped. Would you case to accept my answer then?
Fyodor Soikin
yeah, just have to wait 10 minutes before I can accept...
Peter
Oh, really? Didn't know that. :-)
Fyodor Soikin
+3  A: 

A while ago, I'd written a short function for this:

  /**
   * Gets the query parameters of the given URI as a associative array or map
   * e.g. Given a URI http://www.level3labs.com/jaf?foo=bar&baz=bazzm, will
   * give {"foo":"bar", "baz":"bazzm"}
   * @param {URI} strUri The uri with a query
   * @return Object containing query params or and empty object 
   */
  function getQueryParams(strUri)   {
     var paramMap = {};
     if(!!strUri)   {
        return paramMap;
     }

     var strQuery = strUri.substring(strUri.indexOf("?"));
     if(strQuery && strQuery.indexOf("?") === 0 && strQuery.length > 1) {
        strQuery = strQuery.substring(1);
        var paramValArr = strQuery.split("&");
        for(var i = 0, len = paramValArr.length; i < len; i++)   {
           var paramVal = paramValArr[i];
           var delimIdx = paramVal.indexOf("=");

           var param = null;
           var val = null;
           if(delimIdx >= 0) {
              param = unescape(paramVal.substring(0, delimIdx));
              val = unescape(paramVal.substring(delimIdx + 1));
           }else {
              param = unescape(paramVal);
           }
           paramMap[param] = val;
        }
     }
     return paramMap;
  }
naikus
+1 for the thorough answer
Falle1234
A: 

Try this instead

var link = ($(this).find('a').attr('href')); 
var qs = link.substring( link.indexOf("="));
Falle1234