Eric Müller Eric Müller - 1 year ago 93
Javascript Question

How to check if mithril.js is loaded?

I'm trying to create a local fallback for loading the

file in case the CDN fails.

I want to use the aquivalent of this

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

How do I check if
is already loaded?

(I know of the limits of using this fallback and that it takes a long time to load. Using
is sadly not an option)

Answer Source

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;

e.g. isMithrilLoaded('v.0.2.3');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download