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.


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();


| 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.

| improve this answer | |

You should use use window.parent.location.search 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 = window.parent.location.search.substring(1, window.parent.location.search.length);
    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
| improve this answer | |
  • Why would you use window.parent.location.search? – 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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