0

I have an object Checkout that I invoke from external script, some times when I invoke it, I get erorr masage Checkout is not defined because I think this object loaded before external script. Is there a way to fix this error

external script

<script src="https://example.com/checkout.js"></script>

my script

$( document ).ready(function() {
    Checkout.dosomething();
});
2
  • 1
    You'd be far better off fixing the intermittent loading issue instead of putting a band-aid fix on this. Surely this script is essential for page functionality - what are you expecting to happen if the script isn't available, other than to sweep the error under the rug. If the order of the scripts is the issue, address that instead. Commented Apr 29, 2020 at 15:11
  • please don't forget to accept the answer stackoverflow.com/help/accepted-answer Commented Jun 8, 2020 at 5:34

1 Answer 1

1

The best way would be to order script loading in such a way that the dependant script would be loaded after the dependencies load.

As a workaround, you can check if the dependency is accessible inside your script.

$( document ).ready(function() {
    if (!Checkout) {
        return;
    }
    Checkout.dosomething();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.