0

Externally loading a script, but my script was placed by the client above jQuery (which is a requirement), as such, my script does not work.

I am trying to make my code wait until jQuery has loaded before executing, but I am having difficulty with nested functions within my code; specifically $(x).hover, or $(x).click etc.

I can separate my functions without much trouble, which include jQuery selectors (but they won't be called unless 'x y or z' is done (i.e. until after jQuery is loaded).

I don't know how to have the hover, click etc implemented as they don't work within my $(document).ready(function(){... which is located within the onload yourFunctionName described below - with thanks to user @chaos

Link to onload hook: https://stackoverflow.com/a/807997/1173155

and a quote of the above link:

if(window.attachEvent) {
    window.attachEvent('onload', yourFunctionName);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            yourFunctionName();
        };
        window.onload = newonload;
    } else {
        window.onload = yourFunctionName;
    }
}

Thanks in advance for any help with this.


I have also looked into a loop that checks if jQuery is activated before continueing, but did not implement it as I found that JavaScript does not have a sufficient sleep method that sleeps that specific script.

Community
  • 1
  • 1
shanehoban
  • 870
  • 1
  • 9
  • 30
  • can you post a fiddle? and why it is required to be on top of your `jQuery` script? – Yaje Jun 19 '14 at 08:36
  • @ejay_francisco Apologies, it's not required to be on top, that's just how the client has done so. – shanehoban Jun 19 '14 at 08:37
  • pls post a fiddle so that we can help accordingly – Yaje Jun 19 '14 at 08:39
  • Can't you just tell your client that the script needs to be placed after the jquery line? Seems like the best solution to me. I don't understand why the client would even care. – Joonas Jun 19 '14 at 08:43
  • @Joonas I have done so, but they are quite slow - and they are actually doing us a favour by trialling the software. We are still just taking off, so I would like to try and solve it before they eventually make the necessary change – shanehoban Jun 19 '14 at 08:49

1 Answers1

0

Solution:

if(typeof jQuery === "undefined"){
    if(window.attachEvent) {
        window.attachEvent('onload', myLoadFunction);
    } else {
        if(window.onload) {
            var curronload = window.onload;
            var newonload = function() {
                curronload();
                myLoadFunction();
            };
            window.onload = newonload;
        } else {
            window.onload = myLoadFunction;
        }
    }
}
else {
myLoadFunction();
}
shanehoban
  • 870
  • 1
  • 9
  • 30