2

I'm trying to create a local fallback for loading the mithril.js file in case the CDN fails.

I want to use the aquivalent of this jQuery fallback:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery-2.1.1.min.js"></script>')</script>

How do I check if mithril.js is already loaded?

(I know of the limits of using this fallback and that it takes a long time to load. Using other dependency checker - frameworks is currently not an option)

Marcus
  • 822
  • 1
  • 8
  • 27

1 Answers1

2

If you haven't used any variable called m in your javascript and after your mithril CDN. You can check it by;

if (typeof(m) === 'function') {
  // Mithril is loaded.
}

Or, It's a plus if you know your mithril version.

function isMithrilLoaded(versionName) {
    if (typeof(m) == 'function' && typeof(m.version) == 'function') {
        return m.version() == versionName;
    }
    return false;
}

e.g. isMithrilLoaded('v.0.2.3');
choz
  • 17,242
  • 4
  • 53
  • 73