Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function run_my_code($) {
- // jquery-dependent code here
- $("#foo").data('bar', true);
- }
- var t = null;
- function jquery_ready() {
- if (window.jQuery && window.jQuery.ui) {
- run_my_code(window.jQuery);
- } else {
- t = window.setTimeout(jquery_ready, 100);
- }
- }
- t = window.setTimeout(jquery_ready, 100);
- (function() {
- var runMyCode = function($) {
- // jquery-dependent code here
- $("#foo").data('bar', true);
- };
- var timer = function() {
- if (window.jQuery && window.jQuery.ui) {
- runMyCode(window.jQuery);
- } else {
- window.setTimeout(timer, 100);
- }
- };
- timer();
- })();
- var Namespace = Namespace || { };
- Namespace.Deferred = function () {
- var functions = [];
- var timer = function() {
- if (window.jQuery && window.jQuery.ui) {
- while (functions.length) {
- functions.shift()(window.jQuery);
- }
- } else {
- window.setTimeout(timer, 250);
- }
- };
- timer();
- return {
- execute: function(onJQueryReady) {
- if (window.jQuery && window.jQuery.ui) {
- onJQueryReady(window.jQuery);
- } else {
- functions.push(onJQueryReady);
- }
- }
- };
- }();
- Namespace.Deferred.execute(runMyCode);
- <script>
- if ( ! window.deferAfterjQueryLoaded ) {
- window.deferAfterjQueryLoaded = [];
- Object.defineProperty(window, "$", {
- set: function(value) {
- window.setTimeout(function() {
- $.each(window.deferAfterjQueryLoaded, function(index, fn) {
- fn();
- });
- }, 0);
- Object.defineProperty(window, "$", { value: value });
- },
- configurable: true
- });
- }
- window.deferAfterjQueryLoaded.push(function() {
- //... some code that needs to be run
- });
- </script>
- <script>
- window.deferAfterjQueryLoaded = [];
- window.deferAfterjQueryLoaded.push(function() {
- //... some code that needs to be run
- });
- // ... further down in the page
- window.deferAfterjQueryLoaded.push(function() {
- //... some other code to run
- });
- </script>
- <script src="jquery.js" />
- <script>
- $.each(window.deferAfterjQueryLoaded, function(index, fn) {
- fn();
- });
- </script>
- window.deferAfterjQueryLoaded.push(function() {
- $(function() {
- console.log('jquery loaded and the DOM is ready');
- });
- console.log('jquery loaded');
- });
- function RunAfterjQ(){
- // Codes that uses jQuery
- }
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
- <script type="text/javascript">
- RunAfterjQ();
- </script>
- var afterJQ = [];
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
- <script type="text/javascript">
- for(var i=0; i < afterJQ; i++) afterJQ[i]();
- </script>
- after.JQ.push( function() {
- // this code will execute after jQuery is loaded.
- });
- head.ready(function(){
- $...
- });
- head.js('/jquery.min.js');
- document.addEventListener("DOMContentLoaded", function(event) {
- //you can use jQuery there
- });
- <!-- jQuery defer code body: deferring jQuery calls until jQuery is loaded -->
- <script>
- window.jQueryQ = window.jQueryQ || [];
- window.$ = window.jQuery = function(){
- window.jQueryQ.push(arguments);
- }
- </script>
- <!-- end: jQuery defer code body -->
- <!-- jQuery deferring code footer: add this to the end of body and after the jQuery code -->
- <script>
- jQuery(function(){
- jQuery.each(window.jQueryQ||[],function(i,a){
- // 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
- setTimeout(function(){
- jQuery.apply(this,a);
- },0);
- });
- });
- </script>
- <!-- end: jQuery deferring code footer -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement