Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if Moment js is loaded from CDN

I want to create a fallback if moment js is not loaded from CDN. I couldn't find any helpful resource online, neither on momentjs.com to detect if Moment js is present.

Here's my code :

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<script>
    // If moment.js is not loaded, use the fallback
    if () { 
        document.write('<script src="assets/plugins/moment/moment.min.js"><\/script>');
    }
</script>
like image 205
Garric15 Avatar asked Mar 11 '16 16:03

Garric15


People also ask

Is MomentJS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.

How do I get time from MomentJS?

You can directly call function momentInstance. valueOf(), it will return numeric value of time similar to date. getTime() in native java script.

Is MomentJS still used?

MomentJS is a widely used time and date formatting and calculation library.

Is MomentJS a library?

MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way.


1 Answers

Moment attaches itself to the window when it loads, so you could do:

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
<script>
    if (!window.moment) { 
        document.write('<script src="assets/plugins/moment/moment.min.js"><\/script>');
    }
</script>
like image 110
iamalismith Avatar answered Sep 22 '22 13:09

iamalismith