How to call a common Js Validation file

What doesnt work

I was adding jQuery to a web page. Adding the HTML script tag to the header field doesn’t work within the Website Settings and Web Page (worse: it converts the HTML to text) doctype.


Website settings (doesn’t work)


Web Page (doesn’t work)

Solution

So a workaround was to add it within the Web Page JavaScript field using jQuery.getScript(). I activated caching too in my solution.

$(document).ready(function() {

jQuery.cachedScript = function( url, options ) {
  // Allow user to set any option except for dataType, cache, and url
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });

  return jQuery.ajax( options );
};

$.cachedScript( "https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" )
  .done(function( script, textStatus ) {
    console.log( textStatus );

    // my code goes here

});
});

Credit to @snv

1 Like