Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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_;
      else
        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

3 Answers 3

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

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;    
//console.log(incidentHrefandSupplierID);
$('#WebPartWPQ10 #idHomePageNewItem').attr('href', '#');
$('#WebPartWPQ10 #idHomePageNewItem').attr('onclick', function () {
    return "javascript:showDialog('" +  incidentHrefandSupplierID + "');"
    //console.log(this.onclick);
});

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

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"];
        //console.log(supplierID);

        $().SPServices.SPComplexToSimpleDropdown({
            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();
        $("select[title=SupplierID]").val(selectedSupplierID);
        $("select[title=SupplierID]").change(); 

    });


</script>
share|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
})(jQuery);
share|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
    
Interesting, thanks for the hint!! –  Brenton Pierce Jun 6 '12 at 14:15

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.