QueryString – get parameters from URL with JavaScript

July 23rd, 2007 | Categories: JavaScript, SharePoint | Tags:

SharePoint passes a lot of information between pages with the aid of URL parameters. If you pay attention to the URL while watching the EditForm of some list, you’ll probably find the folloring url:

http://server/site/lists/listname/EditForm.aspx?ID=1&Source=http://server/site/lists/listname/Allitems.aspx

The part after the question mark is important, since it’s passing 2 parameters: ID with value of 1 and Source with value of http://server/site/lists/listname/Allitems.aspx.

Using XSLT with SharePoint Designer you can also get these parameters with XSLT’s QueryString. But outside of a SharePoint Data View you can get the value of URL’s parameters with the following script:

function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split(“&”);
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf(‘=’));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf(‘=’)+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false;
//Here determine return if no parameter is found
  }
}

So if I had my page http://www.mysite.com/default.aspx?test=123

The queryString(‘test’) would return value 123.

This function is useful also outside of SharePoint. One word of caution though: Internet Explorer tends to translate a bit the urls, so spaces become %20 and similar. This can be solved with a simple function to translate these values. More about that – next time :)

  1. Petter
    September 13th, 2007 at 10:35
    Reply | Quote | #1

    It works great! Thanks a million :)

  2. PsychedEric
    November 7th, 2007 at 13:52
    Reply | Quote | #2

    “Using XSLT with SharePoint Designer you can also get these parameters with XSLT’s QueryString.”

    I am a student working for a company, and I am developing under sharepoint designer. I need to pass ( get ) the parameter from the url into an xsl variable.

    I would like to know where I could find these parameters in sharepoint designer.

    In fact I need to replace in an xsl:if the test with ddwrt:Today(), by a date I choose in an input text ( or something else ).

    I hope you have a solution.

    Eric

  3. Boris Gomiunik
    November 18th, 2007 at 14:00
    Reply | Quote | #3

    Hi, PsychedEric. Here’s a reply in an extra post I’ve made:

    Using Parameters from URL as variables in XSLT Data View

    If you need dates, you can even make the web part connection. If you’ll use dates in URL, you have to make it in appropriate format – you can see the format if you enter <xsl:value-of select=”@Created” /> in the XSLT Data View.

  4. naresh
    January 22nd, 2008 at 15:51
    Reply | Quote | #4

    Thanks a lot .it worked.

  5. pushkar
    January 23rd, 2008 at 09:38
    Reply | Quote | #5

    hi,
    i have a calendar list which has recurring and non-recurring tasks.
    i am trying to modify the editform.aspx of the calendar list by making the sharepoint default ListFormWebPart invisible and inserting my custom edit webpart from “insert”-”sharepoint controls”-”custom list form” because i want certain fields to be non-editable.
    the problem is that the tasks can be edited but the daily tasks cant be.If i open to edit the daily task from the calendar it just shows the toolbar. i wanted to know if there’s a way out.
    thanks

  6. Subbiah Mani
    December 8th, 2008 at 05:03
    Reply | Quote | #6

    Great Help !!!!
    Thanks a ton.

  7. vinoth kumar
    January 12th, 2010 at 00:56
    Reply | Quote | #7

    I need help to pass a querystring from sharepoint page to aspx page the querystring doesnot seems to work.

    we pass the url querystring from sharepoint content publishing page to ordinary aspx page.

    also we found that the querysting it not visible correctly in status bar

    expected query string http://domain.com/new.aspx?s=mike

    visible qery string http://domain.com/new.aspx?s=

    is ther any security setting prventing it

  8. Boris Gomiunik
    January 25th, 2010 at 09:42
    Reply | Quote | #8

    @Vinoth Kumar: Hello. This function is a bit obsolete. Can you perhaps try replacing the function with the following one:

    function queryString(paramName) {
    var args = document.location.search.substring(1).split(“&”);
    for(i = 0; i < args.length; i++) {
    nameValues = args[i].split(“=”);
    if(nameValues[0] == paramName) return nameValues[1];
    }
    return null;
    }