Skip to main content

Introducing $(document).ready()


This is the first thing to learn about jQuery: If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

 $(document).ready(function() {
   // put all your jQuery goodness in here.
 });
 
The $(document).ready() function has a ton of advantages over other ways of getting events to work. First of all, you don't have to put any "behavioral" markup in the HTML. You can separate all of your JavaScript/jQuery into a separate file where it's easier to maintain and where it can stay out of the way of the content. I never did like seeing all those "javascript:void()" messages in the status bar when I would hover over a link. That's what happens when you attach the event directly inside an <a href> tag.
On some pages that use traditional JavaScript, you'll see an "onload" attribute in the <body> tag. The problem with this is that it's limited to only one function. Oh yeah, and it adds "behavioral" markup to the content again. Jeremy Keith's excellent book, DOM Scripting, showed me how to create an addLoadEvent function to a separate JavaScript file that allows for multiple functions to be loaded inside it. But it requires a fair amount of code for something that should be rather straightforward. Also, it triggers those events when the window loads, which leads me to another advantage of $(document).ready().
With $(document).ready(), you can get your events to load or fire or whatever you want them to do before the window loads. Everything that you stick inside its brackets is ready to go at the earliest possible moment — as soon as the DOM is registered by the browser, which allows for some nice hiding and showing effects and other stuff immediately when the user first sees the page elements.

Comments

Popular posts from this blog

HTTP Status Code 501 - Not Implemented

501 Error Code Explained The server either does not recognise the request method, or it lacks the ability to fulfill the request. Why it occurs The error occurs when the Web server does not understand or does not support the HTTP method it finds in the HTTP data stream sent to it by the client. The method in the request HTTP data stream should be one of the following GET, OPTIONS, HEAD, POST, PUT, DELETE, TRACE and CONNECT as defined by the HTTP protocol Or the method may be valid but not actually supported by the Web server. Fixing 501 error code The client should specify a valid request type. Even after that if the Web server is responding incorrectly, then the web server simply needs to be upgraded to fix the issue. If you are registered with 100pulse, your site will be monitored and you will be intimated by an email or a short message service when 501 status code error occurs.

Optimize MySQL query with STRAIGHT_JOIN

Before finalizing my queries, I use MySQL Workbench to explain it to make sure the performance is good. I always notice that the performance of the queries with joins on multiple tables varies at times. Then I start indexing my tables to make sure that the Explain is happy and scanning minimum records before handing over the data. But with one of my query, even after adding an appropriate index I was not getting the performance I was expecting. I asked Workbench to explain the query and what? MySQL was not using my newly added index which could optimize the query. I tried FORCE INDEX and MySQL liked it. It started using the index which I forced it to. But I was not happy at forcing MySQL to use my index. I wanted it to do it itself. After researching a bit, I came across STRAIGHT_JOIN and without wasting any time, I added it to my query and what? MySQL started using my index without being forced to. Now the question was, how the STRAIGHT_JOIN works and is different from INNER JOIN. Wit

In Java, what’s the difference between method overloading and method overriding?

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly simple to understand. Let’s start the discussion by talking more about method overloading. Overloading in Java  can  occur when two or more methods in the same class share the same name or even if a child class shares a method with the same name as one of it’s parent classes. But, in order to  actually  have overloaded methods, the methods not only have to have the same name, but there are other conditions that must be satisfied – read below to see what those conditions are. Suppose we have a class called TestClass which has 2 methods, and both methods have the  same  name – let’s say that name is “someMethod” – this would be considered to be method overloading if  at least one of these 2 things is true: 1.) The number of parameters is different for the methods 2.) The parameter types are different. How to NOT overload methods: It’s important to underst