Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. function run_my_code($) {
  2. // jquery-dependent code here
  3. $("#foo").data('bar', true);
  4. }
  5. var t = null;
  6. function jquery_ready() {
  7. if (window.jQuery && window.jQuery.ui) {
  8. run_my_code(window.jQuery);
  9. } else {
  10. t = window.setTimeout(jquery_ready, 100);
  11. }
  12. }
  13. t = window.setTimeout(jquery_ready, 100);
  14.  
  15. (function() {
  16. var runMyCode = function($) {
  17. // jquery-dependent code here
  18. $("#foo").data('bar', true);
  19. };
  20.  
  21. var timer = function() {
  22. if (window.jQuery && window.jQuery.ui) {
  23. runMyCode(window.jQuery);
  24. } else {
  25. window.setTimeout(timer, 100);
  26. }
  27. };
  28. timer();
  29. })();
  30.  
  31. var Namespace = Namespace || { };
  32. Namespace.Deferred = function () {
  33. var functions = [];
  34. var timer = function() {
  35. if (window.jQuery && window.jQuery.ui) {
  36. while (functions.length) {
  37. functions.shift()(window.jQuery);
  38. }
  39. } else {
  40. window.setTimeout(timer, 250);
  41. }
  42. };
  43. timer();
  44. return {
  45. execute: function(onJQueryReady) {
  46. if (window.jQuery && window.jQuery.ui) {
  47. onJQueryReady(window.jQuery);
  48. } else {
  49. functions.push(onJQueryReady);
  50. }
  51. }
  52. };
  53. }();
  54.  
  55. Namespace.Deferred.execute(runMyCode);
  56.  
  57. <script>
  58. if ( ! window.deferAfterjQueryLoaded ) {
  59. window.deferAfterjQueryLoaded = [];
  60. Object.defineProperty(window, "$", {
  61. set: function(value) {
  62. window.setTimeout(function() {
  63. $.each(window.deferAfterjQueryLoaded, function(index, fn) {
  64. fn();
  65. });
  66. }, 0);
  67. Object.defineProperty(window, "$", { value: value });
  68. },
  69.  
  70. configurable: true
  71. });
  72. }
  73.  
  74. window.deferAfterjQueryLoaded.push(function() {
  75. //... some code that needs to be run
  76. });
  77. </script>
  78.  
  79. <script>
  80. window.deferAfterjQueryLoaded = [];
  81. window.deferAfterjQueryLoaded.push(function() {
  82. //... some code that needs to be run
  83. });
  84.  
  85. // ... further down in the page
  86.  
  87. window.deferAfterjQueryLoaded.push(function() {
  88. //... some other code to run
  89. });
  90. </script>
  91.  
  92. <script src="jquery.js" />
  93. <script>
  94. $.each(window.deferAfterjQueryLoaded, function(index, fn) {
  95. fn();
  96. });
  97. </script>
  98.  
  99. window.deferAfterjQueryLoaded.push(function() {
  100. $(function() {
  101. console.log('jquery loaded and the DOM is ready');
  102. });
  103. console.log('jquery loaded');
  104. });
  105.  
  106. function RunAfterjQ(){
  107. // Codes that uses jQuery
  108. }
  109.  
  110. <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
  111. <script type="text/javascript">
  112. RunAfterjQ();
  113. </script>
  114.  
  115. var afterJQ = [];
  116.  
  117. <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
  118. <script type="text/javascript">
  119. for(var i=0; i < afterJQ; i++) afterJQ[i]();
  120. </script>
  121.  
  122. after.JQ.push( function() {
  123. // this code will execute after jQuery is loaded.
  124. });
  125.  
  126. head.ready(function(){
  127. $...
  128. });
  129.  
  130. head.js('/jquery.min.js');
  131.  
  132. document.addEventListener("DOMContentLoaded", function(event) {
  133. //you can use jQuery there
  134. });
  135.  
  136. <!-- jQuery defer code body: deferring jQuery calls until jQuery is loaded -->
  137. <script>
  138. window.jQueryQ = window.jQueryQ || [];
  139. window.$ = window.jQuery = function(){
  140. window.jQueryQ.push(arguments);
  141. }
  142. </script>
  143. <!-- end: jQuery defer code body -->
  144.  
  145. <!-- jQuery deferring code footer: add this to the end of body and after the jQuery code -->
  146. <script>
  147. jQuery(function(){
  148. jQuery.each(window.jQueryQ||[],function(i,a){
  149. // to understand why setTimeout 0 is useful, see: https://www.youtube.com/watch?v=8aGhZQkoFbQ, tldr: having a lot of calls wont freeze the website
  150. setTimeout(function(){
  151. jQuery.apply(this,a);
  152. },0);
  153. });
  154. });
  155. </script>
  156. <!-- end: jQuery deferring code footer -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement