1

Is there a way to define if

https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.0/es5-shim.js

from

https://cdnjs.com/libraries/es5-shim

is loaded or not?

For AngularJS you can check in javascript:

if(!window.angular){
//not loaded
}

What's the proper way of doing that for shim?

Thank you.

Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

3 Answers3

2

Try

<!--[if lt IE 9]>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.0/es5-shim.js"></script>
<script>String.prototype.trim || document.write('<script src="/js/es5-shim.js"><\/script>')</script>
<![endif]-->

or let me know if it isn't work.

YOU
  • 120,166
  • 34
  • 186
  • 219
0

You shouldn't be checking for the presence of a shim. The idea behind using a shim is that you want to use certain methods, e.g. Object.keys. When those methods are not natively available, your shim adds them to the global in question and your code doesn't know the difference.

Can you elaborate on why you'd need to know that it's loaded?

Ben Drucker
  • 71
  • 1
  • 5
  • that's for IE8. If it is not loaded for IE8 from CDN by some reason, then it should be loaded from the local. – Haradzieniec Apr 08 '15 at 14:35
  • In that case you should look at @Raishul's answer or consider using a dedicated script loader that will automatically handle the fallback for you. – Ben Drucker Apr 09 '15 at 14:20
0

Please follow the update section of accepted answer of following SO question:

How to detect if javascript files are loaded?

Following is his solution(All credit goes to T.J Crowder):

function loadScript(path, callback) {

    var done = false;
    var scr = document.createElement('script');

    scr.onload = handleLoad;
    scr.onreadystatechange = handleReadyStateChange;
    scr.onerror = handleError;
    scr.src = path;

    function handleLoad() {
        if (!done) {
            done = true;
            callback(path, "ok");
        }
    }

    function handleReadyStateChange() {
        var state;

        if (!done) {
            state = scr.readyState;
            if (state === "complete") {
                handleLoad();
            }
        }
    }
    function handleError() {
        if (!done) {
            done = true;
            callback(path, "error");
        }
    }
}
Community
  • 1
  • 1
Raishul
  • 146
  • 2
  • 6