tags:

views:

1050

answers:

5

If you view the jquery code below you will see the famous $(document).ready(function(){ that starts the script off. I see this on pretty much all jquery code examples on the net and I am wanting to know, if I am running 5 different code functions in a single file do I need to use $(document).ready(function(){ at the beginning of all of them?

If not how would I combine for example this code below into a page 3 times, pretend it is 3 different codes?

<script type="text/javascript" >
 $(document).ready(function(){
  setTimeout(function(){
  $(".flash").fadeOut("slow", function () {
  $(".flash").remove();
      }); }, 2000);
 });
</script>
+2  A: 

no u dont need , just put it in a single, and trigger all functions from there;

<script type="text/javascript" >
 $(document).ready(function(){
        func1();
        func2();
        ...
});
</script>
Abu Aqil
I am still confused on how to do it though, I mean you say trigger the functions, so I would just call setTimeout(function(){ or does this need to be put into a function, sorry I don't know javascript its much different syntax then php
jasondavis
ok i got it thanks
jasondavis
It's also a good idea to separate out your javascript functions from the page, into a .js file. This generally makes IDEs/editors work better with it, you can minimize all your javascript, etc. When it's all externalized like this, you can just call all your init functions with one block on the page like Abu shows above.
Brian Yarger
+3  A: 

You should try not to put too much inside the doc ready blocks. Yes you can have multiples of them however I stick to one if any. You can also put your script just before the closing body tag without the need for document ready.

I advise breaking the code into separate functions. That way you can reuse them throughout your page at various stages. Then just use the doc ready block to trigger a call to that pages init function.

You can also use $(function(){}); as a shorcut to $(document).ready(function(){});

<script type="text/javascript" >
 $(function(){
    init();
 });

 function init(){
   someFunction();
   //other init stuff
 }

 function someFunction(){
  setTimeout(function(){
     $(".flash").fadeOut("slow", function () {
     $(".flash").remove();
   }); }, 2000);

 }
</script>
redsquare
I don't disagree with reuse at all, but what is gained by using the init function instead of the calling someFunction directly in .ready ?
jfsk3
Say I had a function that setup an accordion and later on in the page cycle I loaded in different content using ajax and needed to re initialize the accordion. If I had stuffed the code in doc ready I could not reuse it.
redsquare
It also helps improve the stack trace when debugging as you get named functions rather than anonymous functions
redsquare
ill buy that, thanks
jfsk3
+1  A: 

First of all, you can shorten that to:

<script type="text/javascript" >
    jQuery(function() {
        ...
    });
</script>

Second, if you want your scripts to run when the page is finished loading, then yes, you need to put them in a jQuery/document.ready() function. You can put them all in the same jQuery(function () { }) block though and they'll be executed in order, you don't have to separate them.

To expand on the workings of function() {} blocks:

jQuery(/* do something */);

means "On page load, do something". That "do something" is a function. You can pass it a function directly like this:

function myFunction() {
    ...
}

jQuery(myFunction);

You defined a function "myFunction" and told jQuery to execute it on page load. Note that you just pass the function itself to jQuery, without (). If you'd write jQuery(myFunction()); instead, that would execute myFunction() immediately, and whatever is returned by myFunction() would be placed in jQuery(), and that would be executed upon page load. It's a little different from languages like PHP, since in PHP it's desired behaviour to execute everything immediately, in Javascript that is not necessarily so. In PHP you can't pass the function itself around, in Javascript you can. Functions in Javascript act a lot more like variables.

What you usually do is "make up a function on the fly" that contains some block of code that you want executed at a later time:

jQuery(function () {
    foo();
    bar();
});

In this case you're passing a function as well, just that you made it up on the fly and the function doesn't have a name. jQuery will hold onto this function until page load, at which time it'll execute it. Inside that function you can do as many things as you like.

deceze
A: 

The problem is that you don't understand what the ready event is and why you need it.

Imagine that a page has not yet loaded fully and you try to change something on it with some javascript and since the code for that HTML element you are trying to manipulate is not even loaded yet, things go bad.

The ready event solves this problem. Any function (most often its an anonymous function) that you bind to the ready event gets executed as soon as all elements in the document are ready to be traversed and manipulated. It's considered bad practice to have any inline javascript is considered bad practice. If you want an event(click,hover,etc) to work on your page, you should call it inside the $(document).ready() function.

And since a page only gets loaded once, any function that is bound to the ready event will only get called once. So there isn't much sense in binding multiple functions to the ready event. You can do everything in that one function that you bind to it. However it will cause no harm (as long as you understand what you are doing) since every function that you have bound to the ready event will be called once the DOM is ready.

I don't understand what you are trying to achieve by running that same piece of code five times... so I can't help you with that.

I hope that this explanation helps you solve your actual problem.

Kalmi
A: 

OK ... so how would I make the following work? As it is now, the first part works fine but the second part (nonMemDel) does not.

$(document).ready(function() {

$.viewMemDel = { '0' : $([]), '1memdel' : $('#1memdel'), '2memdel' : $('#2memdel'), '3memdel' : $('#3memdel'), '4memdel' : $('#4memdel'), '5memdel' : $('#5memdel'), '6memdel' : $('#6memdel'), '7memdel' : $('#7memdel'), '8memdel' : $('#8memdel'), '9memdel' : $('#9memdel'), '10memdel' : $('#10memdel')

};

$('#number_of_199_tix').change(function() { // hide all $.each($.viewMemDel, function() { this.hide(); }); // show current $.viewMemDel[$(this).val()].show(); });

$.viewNonMemDel = { '0' : $([]), '1nonMemdel' : $('#1nonMemdel'), '2nonMemdel' : $('#2nonMemdel'), '3nonMemdel' : $('#3nonMemdel'), '4nonMemdel' : $('#4nonMemdel'), '5nonMemdel' : $('#5nonMemdel'), '6nonMemdel' : $('#6nonMemdel'), '7nonMemdel' : $('#7nonMemdel'), '8nonMemdel' : $('#8nonMemdel'), '9nonMemdel' : $('#9nonMemdel'), '10nonMemdel' : $('#10nonMemdel')

};

$('#number_of_249_tix').change(function() { // hide all $.each($.viewNonMemDel, function() { this.hide(); }); // show current $.viewNonMemDel[$(this).val()].show(); });

});

These are called by a select element farther down the page which should show or hide a block of form elements.

TIA

Dave

Dave
OK .. now I am a bit more confused by your answer ... but I appreciate your taking the time.
Dave
I do not want ANYTHING to happen when the page loads, onlu when a user makes a selection from one of two select elements in a form farther down. Based on those selections, divs with more form elements will be shown to the user
Dave