JQuery best practice, using $(document).ready inside an IIFE?

No, IIFE doesn’t execute the code in document ready. 1. Just in IIFE: (function($) { console.log(‘logs immediately’); })(jQuery); This code runs immediately logs “logs immediately” without document is ready. 2. Within ready: (function($) { $(document).ready(function(){ console.log(‘logs after ready’); }); })(jQuery); Runs the code immediately and waits for document ready and logs “logs after ready”. This … Read more

jQuery document ready function

All three of the following syntaxes are equivalent: $(document).ready(handler) $().ready(handler) (this is not recommended) $(handler) Aliasing the jQuery Namespace When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would … Read more

How to run a jquery function in Angular 2 after every component finish loading

You will want to use the “ngAfterViewInit” lifecycle hook, through importing AfterViewInit (https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#afterview). You can use it as shown below: Installation: tsd install jquery –save or typings install dt~jquery –global –save Utilization: import { Component, AfterViewInit } from ‘@angular/core’; import * as $ from ‘jquery’; ngAfterViewInit() { this.doJqueryLoad(); this.doClassicLoad(); $(this.el.nativeElement) .chosen() .on(‘change’, (e, args) => … Read more

jQuery mobile $(document).ready equivalent

I spent some time on researching the same since JQM docs are not very detailed at this point. Solution below works fine for me: <script type=”text/javascript”> $(‘div:jqmData(role=”page”)’).live(‘pagebeforeshow’,function(){ // code to execute on each page change }); </script> You have to implement your own checking flow in order to prevent multiple initialization since the code above … Read more

How can I run a directive after the dom has finished rendering?

It depends on how your $(‘site-header’) is constructed. You can try to use $timeout with 0 delay. Something like: return function(scope, element, attrs) { $timeout(function(){ $(‘.main’).height( $(‘.site-header’).height() – $(‘.site-footer’).height() ); }); } Explanations how it works: one, two. Don’t forget to inject $timeout in your directive: .directive(‘sticky’, function($timeout)