1

Is it possible, in javascript, to defer the execution of a self-execution function that resides at the top level closure? (For you to understand, jQuery uses this. It has a self-execution function at global scope)

My objective here is to make the whole external script to execute at a later time without deferring it's download, such that it downloads as soon as possible and executes after an event (I decide when that event happens).

I found this also:
https://developer.mozilla.org/en-US/docs/DOM/element.onbeforescriptexecute

Sounds like a "part 1" of what I want. If I cancel, the script is not executed. but then... How do I effectively execute it later?

Note1: I have absolutely no control over that external script, so I cannot change it.

Note2: Both me and who requested this does not care if there's slowness in IE due to stuff like this (yeah, true! (s)he's anti-IE like me)

Edit (additional information):
@SLaks The server sends the default headers that apache send (so-it-seems) plus a cache-control header ordering to cache for 12 hours.
From the POV of the rules about domains, it is cross-domain.
I'm not allowed to change the definitions about the other server. I can try to asking for it, though.

brunoais
  • 4,743
  • 7
  • 33
  • 55

1 Answers1

1

Unfortunately it's not possible to do with the <script> tag because you can't get the text of an external script included that way to wrap it in code to delay it. If you could, that would open up a whole new world of possibilities with Cross Site Request Forgery (CSRF).

However, you can fetch the script with an AJAX request as long as it's hosted at the same domain name. Then you can delay its execution.

$.ajax({
    url: '/path/to/myscript.js',
    success: function(data) {
        // Replace this with whatever you're waiting for to delay it
        $.ready(function() {
            eval(data);
        });
    }
});

EDIT I've never used $.getScript() myself since I use RequireJS for including scripts, so I don't know if it will help with what you need, but have a look at this: http://api.jquery.com/jQuery.getScript/

FYI If you want to know what I'm talking about with the CSRF attacks, Check this out.

Community
  • 1
  • 1
Justin Warkentin
  • 8,501
  • 4
  • 30
  • 32
  • I don't use jQuery, but I know what that code does and I can translate it to a native DOM. – brunoais Feb 07 '13 at 16:54
  • @brunoais: I doubt you can translate `jQuery.getScript` to native functions easily :-) – Bergi Feb 07 '13 at 17:15
  • @Bergi You don't have to use `getScript`, as I domonstrated. It's not really hard though. All you have to do is a normal XMLHttpRequest without the extra help of jQuery. It's still not that hard to do. If it would be more helpful I could translate this to vanilla JS for you. @brunoais If this solves your problem don't forget to mark it :) Thanks! – Justin Warkentin Feb 07 '13 at 17:54
  • @Justin: Thanks no, I know how it would look like with vanilla JS. Yet I know as well how `getScript` is implemented, and it could get complicated if you wanted to replicate all of its anti-quirks behaviour :-) – Bergi Feb 07 '13 at 18:02
  • @Bergi getScript() does not solve, anyway. I'll use something similar to how RequireJS does, but much more simplified. I don't need this fully optimized for IE < 9 (I just needs to work somehow) and I'd rather not have more scripts loaded into the page. 5 is already more than enough... – brunoais Feb 07 '13 at 18:17
  • @JustinWarkentin I'll give more time to this question. There might be better answers in the meantime.... – brunoais Feb 07 '13 at 18:20