MaxLuc
MaxLuc

Reputation: 33

Load jquery conditionally with pure JavaScript if windows with < than 600px

Is there a way to load jquery conditionally with pure JavaScript if windows with < than 600px and to put an "else" that load jquery in a async way?

Upvotes: 1

Views: 66

Answers (1)

yezzz
yezzz

Reputation: 3020

Something like this?

var sync;
if (window.outerWidth < 600) {
  sync = false;
} else {
  sync = true;
}

function loadScript(sync) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.async = sync;
  script.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.js";
  document.getElementsByTagName("head")[0].appendChild(script);
};
loadScript(sync);

Upvotes: 1

Related Questions