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)