0

I'm looking for a way to access the Query String of a page that has been loaded into a div, through jquery or javascript.

The reason is that I need to modify how the page calls a function based on whether a parameter has been loaded into the page or not.

I've tried this inside the page that has been loaded into the div

var fileIDPresent = false;
var url = this.window.location.href;
alert(url);
if (url.indexOf('?' + 'FileID' + '=') != -1)
   fileIDPresent = true;
else if (url.indexOf('&' + 'FileID' + '=') != -1)
   fileIDPresent = true;

The page is loaded into the div through the following code:

 $('#loadedContentHolder').load('/ViewDetails.aspx?FileID=' + File, function () {
                // load finished...
                $('#loadedContentHolder').slideDown('slow');
            });
S Grimminck
  • 516
  • 4
  • 21
  • 1
    Please describe what your problem is! – Vincenzo Maggio Sep 07 '12 at 14:10
  • How is the page "loaded in to a DIV"? If it is an iframe, then you can check the `src` of the iframe. If the content has been added as children of the DIV, then the source information is lost. – Orbling Sep 07 '12 at 14:11
  • See if this helps: http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery ...It seems like you're actually looking for a way to parse the url string for the query params. However, it's a bit difficult to discern from the OP. – Chase Sep 07 '12 at 14:12

1 Answers1

1

The URL is lost when you inject the code into the DIV. You need to save the URL to the div node somehow.

You could do this:

var url = '/ViewDetails.aspx?FileID=' + File;
$('#loadedContentHolder').load(url, function () {
  // load finished...
  $('#loadedContentHolder').data('url', url).slideDown('slow');
});

Next time you want to know what URL was used you can fetch the url like this:

var url = $('#loadedContentHolder').data('url');

This will return the URL that was used and you can use that url to extract the params:

var params = getUrlParams(url);

function getUrlParams(url) {
  var vars = [], hash
      ,hashes = url.indexOf('?') + 1).split('&');

  for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    if (hash.length === 2) {
        vars.push(hash[0]);
        vars[hash[0]] = unescape(hash[1]);
    }
  }
  return vars;
}
Koen Peters
  • 12,798
  • 6
  • 36
  • 59
  • The problem is that the calling window.location.href points the page that contains the parent div, which does not have the query string parameters in it... – S Grimminck Sep 07 '12 at 14:16
  • So you inject HTML into a div using an AJAX request and you want to know what the query strings were of theat Ajax call? Show us some code of how you inject the HTML into the div. I think that will help us to understand your problem – Koen Peters Sep 07 '12 at 14:18
  • posted code snippet in original post to show how it was loaded into the div. – S Grimminck Sep 07 '12 at 14:20
  • I changed my answer. Maybe that helps – Koen Peters Sep 07 '12 at 14:28
  • it does thanks. I'm now trying to access that url param from inside the loaded page. – S Grimminck Sep 07 '12 at 14:43