Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Friday, 30 May 2014

jQuery window.load() vs document.ready()

 
$(window).load(function () {

vs 

$(function() {
$( document ).ready(function() {


ready() executes when the DOM is fully loaded (see http://api.jquery.com/ready/).  
load() Executes always later than ready() when all the content (images, css, etc) has been loaded

Had problem when showing a confirmation dialog when the page was rendered; images where not loaded!

Thursday, 16 February 2012

Javascript:events: onbeforeunload event

Usage: When closing the tab/browser the onbeforeunload event is triggered. We can confirm the exit or abort it.
<html>
<head>

<script type="text/javascript">
window.onbeforeunload = function() {
  return "dummy string; it won't appear to user...";
}
</script>


</head>


<body>

<p>When closing the tab/browser the onbeforeunload event
is triggered...close the tab/browser....</p>


</body>

</html>

Integrating jQuery with Prototype

The problem Both libraries, i.e. jQuery and Prototype use the character $ with special meaning...

The solution: See jQuery: what if another javascript library uses $?

jQuery: what if another javascript library uses $?

Background: jQuery uses $() as an alias to the function jQuery().

The problem:What if another library uses the same character? (e.g. Prototype; other libraries???)

Solution:Make jQuery not use to ${} by calling $.noConflict()

Code taken from jQuery.noConflict():
<script type="text/javascript">
  $.noConflict();
  // Code that uses other library's $ can follow here.
</script>

References

Monday, 13 February 2012

jQuery: check if a variable is defined

if($('#foo').length > 0) { alert("id 'foo' exists!"); } else { alert("id 'foo' doesn't exist!"); }

References

See also how to do the same with Javascript.

Javascript: check if a variable is defined

var foo = 1; //variable exists

if(! typeof foo == 'undefined') {
  alert("var 'foo' exists!");
}
else {
  alert("var 'foo' doesn't ex
ist!"); }

References

See also how to do the same with jQuery.

jQuery: How to animate opacity (from hidden to visible)

Assuming we want to animate all the divs...and that the initial opacity of the divs is 0 (not visible):
var $elem = $('.div');
$elem.animate({'opacity':1},800,function(){
 //      
});

References

Method signature:
.animate( properties [, duration] [, easing] [, complete] )

where

* properties: A map of CSS properties that the animation will move toward.
* duration: A string or number determining how long the animation will run.
* easing: A string indicating which easing function to use for the transition.
* complete: A function to call once the animation is complete.