0

I am using jQuery load() to load a partial page into a div. I want to pass url parameters to the partial page like so

<div id='partial'></div>
<script>
$('#partial').load('child_page.html?key=1');
</script>

How do I access these parameters from inside the child page? I tried the following in the child page, but the issue is that the window.location is not giving me child_page.html?key=1 but is giving me the root page url.

<script>
var url = window.location;
//code to parse the url to extract the parameters goes here
</script>
Alex H
  • 1,424
  • 1
  • 11
  • 25
hmahidhara
  • 13
  • 1
  • 7
  • _"//code to parse the url to extract the parameters goes here"_ `.load()` not appear to change `window.location` ? – guest271314 Jul 12 '15 at 15:09
  • "child page" is not really a page, it is just content being added into the current page. Please refer to my new answer for details. – alan0xd7 Jul 12 '15 at 16:06
  • @alan0xd7 _""child page" is not really a page, it is just content being added into the current page. Please refer to my new answer for details."_ Yes , though that does not appear to be actual Question at original post _"How to access URL parameters sent to div via jQuery load()"_ ? That is, how to access "parameters" passed `.load()` ? _"from inside the child page?"_ would be same `html` page `.load()` called from – guest271314 Jul 12 '15 at 17:12

3 Answers3

3

It is not possible for the "child page" to have its own DOM and scripting context, because it was simply retrieved as a string (via AJAX) and inserted into the parent page. Therefore it does not have its own window.location. Everything is added to the DOM of the parent page, and scripts are executed in the context of the parent page.

If you want to share variables between your parent page and the new script loaded with load, you can attach the variable to window and then access it in the script of the "child page".

For example:

// parent page
window.sharedVariable = "Hello World!";
$('#partial').load(url);

// child page
var v = window.sharedVariable;

Working demo (child page)

Update:

Here is an alternate method to support multiple partials, using data attributes to share the variables.

// parent page
$('#partial_1').load(url).data("param", "111");
$('#partial_2').load(url).data("param", "222");
$('#partial_3').load(url).data("param", "333");

// child page
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.parentNode;
var param = $(parentTag).data("param");

Basically, we set the data we want to share in the partial's div. And then in the "child page", we find the current running script tag, then the parent tag would be the div that has the data in it.

Working demo (child page)

alan0xd7
  • 1,831
  • 1
  • 15
  • 19
  • This answer does not appear to address "URL parameters" described at original post ? – guest271314 Jul 12 '15 at 17:04
  • The direct answer is "it cannot be done" because there is no "child page", it is all just part of the same page. – alan0xd7 Jul 12 '15 at 17:08
  • @alan0xd7 Used the approach you are suggesting in other areas of the application where the data to be shared between pages was static and known in advance. But in this case the number of partial divs will be known only at run time: parent_page.html has the following js
    child_page.html needs to be loaded into multiple divs present in parent_page.html. Inside child_page.html want to access key value sent to it via load.
    – hmahidhara Jul 12 '15 at 17:12
  • Yes. Solution should return parameters passed to `.load()` within `.load()` `complete` callback . If parameters passed to `.load()` is changed , those changed parameters should be accessible within each call to `.load()` – guest271314 Jul 12 '15 at 17:15
  • I have provided an alternate method that supports multiple partials, please take a look. – alan0xd7 Jul 12 '15 at 17:27
  • @alan0xd7 The solution using data object to transport the params worked well for me. Thanks. – hmahidhara Jul 13 '15 at 18:07
  • @alan0xd7: Thanks so much. This solution works for me. – Loc Huynh Nov 15 '16 at 23:37
1

This takes a slightly different course, but achieves the result you're looking for just as well. You can use the HTML5 History API and do something like this:

history.replaceState(null, null, '?key=1');
var test = $('#partial').load('two.html');

Then from two.html, the window.location.search call will give you ?key=1 and you can parse the query string from there..

Graham T
  • 968
  • 9
  • 14
  • Nice and simple. Also allows the second page (two.html) to load without any code edits. This should be the top answer. – user2205930 Jun 30 '21 at 05:36
0

How to access URL parameters sent to div via jQuery load()

Edit, Updated

Note, original Question does not appear to include description of script element expected , to run, within returned html from call to .load()


Try utilizing beforeSend to set settings url at jqxhr object , access jqxhr object within .load() complete callback

var url = "https://gist.githubusercontent.com/anonymous/4e9213856be834b88f90/raw/1d90ab5df79e6ee081db2ff8b94806ef27cbd422/abc.html";

$.ajaxSetup({
  beforeSend: function(jqxhr, settings) {
    jqxhr.params = settings.url
  }
});

function loadComplete(data, textStatus, jqxhr) {
    $(this).find("h1")
    .html(function(_, html) {
      return html + " My param is " + jqxhr.params.split("?")[1]
    });
};

$("#partial_1").load(url + "?111", loadComplete);

$("#partial_2").load(url + "?222", loadComplete);

$("#partial_3").load(url).load(url + "?333", loadComplete);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

</script>
<div id="partial_1"></div>
<div id="partial_2"></div>
<div id="partial_3"></div>

jsfiddle http://jsfiddle.net/L9kumjmo/5/

guest271314
  • 1
  • 15
  • 104
  • 177
  • May I ask, why would you actually want to do this? If you are going to request the page, you already know the URL (and its parameters) beforehand, couldn't you just save that instead? – alan0xd7 Jul 12 '15 at 15:01
  • @alan0xd7 If `url` , parameters not known ? , but created , passed to `.load()` dynamically ? – guest271314 Jul 12 '15 at 15:05
  • Even if you create the parameters dynamically, it is still known the moment you call `load(url)`, as that is part of the `url`. – alan0xd7 Jul 12 '15 at 15:08
  • @alan0xd7 How would it be known when asynchronous `.load()` completes to access parameters passed ? What is issue with defining , attaching property to `jqxhr` object ? – guest271314 Jul 12 '15 at 15:12
  • When you call `load()`, you give it a url, correct? So in that url, you already have the parameters, no? – alan0xd7 Jul 12 '15 at 15:41
  • @guest271314 In your solution to attach the property to jqxhr, how do I access the jqxhr and the property in the child page? If I understanding correctly, in your example you are accessing jqxhr inside the parent page itself. – hmahidhara Jul 12 '15 at 15:42
  • @hmahidhara What is _"child page"_ ? `window.location` does not appear to be changed by `js` at Question ? See http://stackoverflow.com/questions/31368864/how-to-access-url-parameters-sent-to-div-via-jquery-load/31369168?noredirect=1#comment50717794_31368864 – guest271314 Jul 12 '15 at 15:44
  • @guest271314 parent_page.html has the following js
    child_page.html needs to be loaded into multiple divs present in parent_page.html. Inside child_page.html I want to access the key value sent to it via load and and take appropriate actions based on it. Now if I use your approach to set the parameter (key) in jqxhr, how do i extract it in child_page.html?
    – hmahidhara Jul 12 '15 at 17:03
  • @hmahidhara Would appear to be same `DOM` ? `jqxhr.params` could be accessed in callback of `.load()` as attempted to demonstrate at answer – guest271314 Jul 12 '15 at 17:07
  • I have provided an alternate method that supports multiple partials, please take a look. – alan0xd7 Jul 12 '15 at 17:36
  • @alan0xd7 Yes, solution above return expected results as well http://jsfiddle.net/L9kumjmo/5/ – guest271314 Jul 12 '15 at 17:47
  • Sorry my last comment was for the original question, not directed at your answer. Got confused because this is getting very looong. – alan0xd7 Jul 12 '15 at 17:57