In a php file i have used include
to include the following js.php file
and prior to that i have included the jquery file.
instead of $( document ).ready( function() { ... } );
try
$( document ).ready( abc() );
function abc() { .... }
worked for me.i myself searched lot of places, but couldn't find a solution. Tried this out randomly and worked!!
In my case document.ready was not called because a html form was sent to server by Ajax request and only part of document was recived - and document ready is not called after ajax request.
In consequence some buttons which were on that part that was reloaded was not binded to events .click() atc.. anymore because these buttons were new and binding of these events was called during document ready only first time - not for AJAX.
I faced a similar problem too and this is what I did.
Ensure that document.ready is placed after the callback function is defined in your javascript file.
Here, init is not defined yet when document.ready is being parsed.
//won't work
$(document).ready(init);
var init = function() {
console.log('init not called');
}
Correct method:
//Works
var init = function() {
console.log('document is ready');
}
$(document).ready(init);
Notice that $(document).ready(init) is placed at the end.
Following Zakas' The art of throwing JavaScript errors, I began to throw my own errors in JavaScript as an approach to finding JS issues.
But this approach unfortunately started causing the jQuery ready() functions to silently fail, for unrelated parts of the site. :(
I learned that the following are different:
throw new Error()
is different from
console.error()
So, I started to use console.error(), and I still got the red highlighting in the console.
$(function(){ alert(...); });
just in case...The most likely answer, based on what you have said, is that the core jQuery file is not actually included correctly in the page. You need something like:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
Chances are, this is missing or typed incorrectly.