1

My page content.html is called from another page.My question is that there are only AJAx calls made in our page.So in my page i notice that ready function is not called every time.I have to clear my cache every time to get into ready function.How to resolve this.Also if there is any ready function included in a base page and this is included every where .Should we append our code to this function.Or is it that a page can have many ready functions

$(document).ready(function() {
// Do this immediately if DOM is loaded, or once it's loaded otherwise.
});
heisenberg
  • 9,665
  • 1
  • 30
  • 38
Rajeev
  • 44,985
  • 76
  • 186
  • 285

3 Answers3

1

A single page may have multiple ready functions. jQuery is binding the function you pass it to the "ready" event. Thus if you bind three functions to the event, all three will get called when it is fired.

The ready event itself, is fired every time the DOM is ready. This happens one time per page view, and should not require you to clear your cache in order for it to fire.

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
  • How do u account for a ready function not being called in this case? – Rajeev Jan 07 '11 at 20:05
  • Typically you *should* only have one ready binding because you will only ever need 1. What are you expecting to happen in your code execution? – Marcus Whybrow Jan 07 '11 at 20:07
  • For testing purpose i just put an alert on ready and the behavior is that some times it gets called and some time it is not. – Rajeev Jan 07 '11 at 20:07
  • Could you try to replicate the problem using the tools at http://jsfiddle.net. I have never encountered the ready event *not* fireing, It is more likely that jQuery might not be loading correctly in some cases. – Marcus Whybrow Jan 07 '11 at 20:08
  • No it cannot be , including those AJAx functions is complicated.Also when a jquery(jquery.js) file is included in our page.Is it mandatory to be always to be included in the head tag? – Rajeev Jan 07 '11 at 20:10
  • These AJAX functions you are talking about seems irrelevant to the problem. The ready function and an alert appear to be the crux of your question, remember invalid code case cause JavaScript to stop working, but since you won't show me your code I can do no more than state how the ready function works. – Marcus Whybrow Jan 07 '11 at 20:16
  • An example of where a ready will not fire again is when you are using Struts 2 and have and submit a form with an action that points to the same page. However, this is not a case of sometimes yes or sometimes no. The first visit always calls the ready, the submit button never calls the ready. – demongolem Nov 02 '11 at 17:10
1

Marcus is right when he says you can have multiple ready functions in a single page. However, a ready function is run when the document is ready. As a result, once that has happened, you cannot expect the ready function to run again until you reload the page (which is what you are probably seeing).

JasCav
  • 34,458
  • 20
  • 113
  • 170
1
$().ready(initializationFunction);

//call this in your ajax callback
initializationFunction(jQuery);

Source: How to trigger $().ready() in jQuery?

initializationFunction is the function that would normally be stored in the ready event.

Community
  • 1
  • 1
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73