The task is:
After the page loads, check whether jQuery is connected, load it in case of unavailability. After that, call the function myfunc on jQuery.

 function myfunc() { jQuery("<myselector>").contents(); }; if (window.jQuery) { myfunc(); } else { script = document.createElement('script'); script.onload = function() { setTimeout(myfunc, 3000); }; script.type = 'text/javascript'; script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'; document.head.appendChild(script); } 

In the Google Chrome console, I run this code for the loaded page.
I get an error in reply:

Uncaught ReferenceError: jQuery is not defined

Why does an error occur? After all, I make the myfunc function call after the page is fully loaded and jquery.min.js , and even with a delay of 3 seconds. And in some cases, downloading the same site, this error does not occur.

Tested on techcrunc.

  • at the end of the body script added? This construction works for me - Jean-Claude
  • I run this code through the browser console manually, after loading the page itself - Ivan Myasnikov

0