5

I am building an ASP-NET CORE 3.1 MVC Web Application. I wish to use the CDNJS network to provide my script resource files, and since this is a production application, I must have a fallback to a local copy for when the CDN might be offline. This is a fairly basic task and I can successfully load multiple libraries such as Bootstrap 4.5, jQuery 3.3, etc.

As you may know, ASPNET core script tag-helpers have awesome functions built in to do what I want to do automatically. When trying to load jquery.unobtrusive-ajax.min.js I am unable to decide what my fallback test function should actually be. The ????? below represents the code I cannot make work.

Basically, I need to write JS code that returns false when the CDN is not available. This code will be plugged into the ?????.

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.min.js"
        asp-fallback-src="~/lib/jquery-ajax-unobtrusive/jquery.unobtrusive-ajax.js"
        asp-fallback-test="window.jQuery && ?????">
</script>
laylarenee
  • 3,276
  • 7
  • 32
  • 40

3 Answers3

3

As far as I know, the fallback-test attribute a Javascript expression to use for the fallback test. Should resolve to true if the primary script loads successfully.

After check the jquery.validate.unobtrusive.js, we could find it depend the jQuery.validator js.

So I suggest you could add below expression:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.min.js"
        asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
         >
</script>
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • 3
    This is the fallback for unobtrusive validation plugin `jquery.validate.unobtrusive.min.js` ... I need the one for unobtrusive AJAX – laylarenee Jul 09 '20 at 12:30
2

Just discovered this post while looking for the same answer.

I found that

asp-fallback-test="getEvents($(document)[0]).submit"

Worked, where getEvents is defined as follows (from https://stackoverflow.com/a/12899236/4254458)

    function getEvents(element) {
        var elemEvents = $._data(element, "events");
        var allDocEvnts = $._data(document, "events");
        for (var evntType in allDocEvnts) {
            if (allDocEvnts.hasOwnProperty(evntType)) {
                var evts = allDocEvnts[evntType];
                for (var i = 0; i < evts.length; i++) {
                    if ($(element).is(evts[i].selector)) {
                        if (elemEvents == null) {
                            elemEvents = {};
                        }
                        if (!elemEvents.hasOwnProperty(evntType)) {
                            elemEvents[evntType] = [];
                        }
                        elemEvents[evntType].push(evts[i]);
                    }
                }
            }
        }
        return elemEvents;
    }
  • Simplified - asp-fallback-test: "window.jQuery && window.jQuery._data(window.jQuery(document)[0], 'events') && window.jQuery._data(window.jQuery(document)[0], 'events').submit" – Rkand Dec 21 '21 at 18:58
0

You can also use the following:

<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"  
  asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"  
  asp-fallback-test="window.jQuery && window.jQuery.validator"  
  crossorigin="anonymous"  
  integrity="sha384-Fnqn3nxp3506LP/7Y3j/25BlWeA3PXTyT1l78LjECcPaKCV12TsZP7yyMxOe/G/k">  
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"  
  asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"  
  asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"  
  crossorigin="anonymous"  
  integrity="sha384-JrXK+k53HACyavUKOsL+NkmSesD2P+73eDMrbTtTk0h4RmOF8hF8apPlkp26JlyH">  
Gauravsa
  • 6,330
  • 2
  • 21
  • 30