-2

I may be thinking about this the wrong way, but what I'm trying to do is load a region of a single page web-app using something like Ajax or the jQuery .load function, but load it such that when the section loads, this does not refer to the entire global application scope, but rather just the segment that was loaded. Basically, I want the region to be able to manipulate itself, but not have any access to outside objects (at least not unintentionally).

tereško
  • 58,060
  • 25
  • 98
  • 150
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
  • 4
    That's what ` – Pointy Jul 08 '13 at 15:26
  • Also: when you load your content, the *best* thing would be for it to have no ` – Pointy Jul 08 '13 at 15:36
  • How would the loaded page communicate to the container page what scripts it wanted? – sircodesalot Jul 08 '13 at 15:38
  • Well there are various ways you could do that. The containery framework could dynamically load services via some basic API, or else (depends on circumstance) the container could simply have available all services that any part of the site needs. – Pointy Jul 08 '13 at 16:17
  • I would be careful with iFrames. See http://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice for a discussion on them. – vee Jul 09 '13 at 07:42

1 Answers1

3

jQuery's ajax(...) method has a context parameter that you can use to specify what this will be within any of the callbacks.

To quote their documentation:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $(this).addClass("done");
});

See http://api.jquery.com/jQuery.ajax for more details.

vee
  • 1,247
  • 16
  • 18
  • Oh, that's pretty useful. Thank you! – sircodesalot Jul 08 '13 at 15:34
  • This is true and it's a good thing to know, but it won't really achieve what was asked for. – Pointy Jul 08 '13 at 15:35
  • Well the OP is (I think) about how to constrain scripts (like, code in ` – Pointy Jul 08 '13 at 16:16
  • I interpreted it having `this` to refer to something other than the `window` object, which I saw as the "global application scope" that was mentioned. With the approach above, you can specify any object you want, including the DOM element that will have it's HTML content replaced on an AJAX success callback. I guess we'd have to get some more clarification from the OP to see exactly what he's looking for here. – vee Jul 09 '13 at 07:40
  • Yeah, I'm just trying to load a section of the page in sort of a 'sandboxed' mode, where the `this` object refers to the top level html element of the sandbox, rather than the entire `window`. – sircodesalot Jul 09 '13 at 15:45