SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Not sure if it is possible using SPServices or not, or if I have to roll my own. I need to get the querystring from a parent window when using a modal to add/edit a document. Thoughts?

I could use:

function getQuerystring(key, default_)
      if (default_== null) default_=""; 
      key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
      var qs = regex.exec(unescape(window.parent.location.href));
      if(qs == null)
        return default_;
        return qs[1];

    var proj = getQuerystring('proj');

But if I can i would prefer to use SPServices since I am already including that as a reference in my master page.

share|improve this question

I put this in the parent window to get the query string from the URL and pass it to the Add new item link on the list view web part

<script type="text/javascript">
$(document).ready(function() {

//Gets the supplierID from the URL 
var queryStringVals = $().SPServices.SPGetQueryString();
var supplierID = queryStringVals["FilterValue1"];

//replaces the onlick attribute of the add new item link with a NewForm.aspx and my query string variable
var incidentHrefandSupplierID = "http://sp2010/sites/MySite/Lists/MyList/NewForm.aspx?SupplierID=" + supplierID;    
$('#WebPartWPQ10 #idHomePageNewItem').attr('href', '#');
$('#WebPartWPQ10 #idHomePageNewItem').attr('onclick', function () {
    return "javascript:showDialog('" +  incidentHrefandSupplierID + "');"

//This function lives outside document.ready
function showDialog(url) { 
var options = {
title: "New Item",
width: 800,
height: 600,
url: url };

In the newform.aspx, I used this to get the query string and populate a lookup column.

<script type="text/javascript">
    //Gets the supplierID from the URL and adds it to the Add new item link

    $(document).ready(function() {

        var queryStringVals = $().SPServices.SPGetQueryString();
        var supplierID = queryStringVals["SupplierID"];

            columnName: "SupplierID",

        //like :contains but matches the text exactly
        $.expr[':'].textEquals = function(a, i, m) {
            return $(a).text().match("^" + m[3] + "$");

        var selectedSupplierID = $("option:textEquals('" + supplierID + "')").val();


share|improve this answer

SPServices has it's own query string parser operation that is easy to use. You could parse it and throw it in as an arguement in your modal open function to be able to leverage it.

share|improve this answer

You should use use instead of window.parent.location.href

It would also be possible to create a function like:

(function ($) {
  // Get the Query String parameters and their values and return in an array
  $.fn.MySPServices.SPGetParentQueryString = function () {
    var i;
    var queryStringVals = {};
    var qs =,;
    var args = qs.split("&");
    var rxQS = /^([^=]+)=(.*)/i;
    for (i=0; i < args.length; i++) {
      matches = rxQS.exec(args[i]);
      if (rxQS.test(window.parent.location.href)) {
        if (matches !== null && matches.length > 2) {
          queryStringVals[matches[1]] = unescape(matches2]).replace('+', ' ');
    return queryStringVals;
  }; // End $.fn.MySPServices.SPGetParentQueryString
share|improve this answer
Why would you use – Brenton Pierce Jun 5 '12 at 20:44
Because that's the property that only inclyudes the Query part of the URL – Per Jakobsen Jun 6 '12 at 3:54
Interesting, thanks for the hint!! – Brenton Pierce Jun 6 '12 at 14:15

Your Answer


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.