This is my code:
$(document).ready(function() {
alert("I am an alert box!");
});
when I refresh the page, there is no alert box. Is there any wrong??
jQuery seems to be added correctly, but I am not able to get the alert for checking whether jQuery is working. Any ideas on how to make the alert show?
ready is not a function" error occurs for multiple reasons: Placing second set of parenthesis after the call to the ready() method. Loading the jQuery library after running your JavaScript code. Forgetting to load the jQuery library.
The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.
There is also $(document). on( "ready", handler ) , deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.
So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.
Did you include the jQuery library before the call in your document?
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
...
<script type="text/javascript">
$(document).ready(function() {
alert("I am an alert box!");
});
</script>
That should work provided you've loaded jQuery and you haven't used noConflict
. It works here, for instance.
Without more information we can't really help, but here are some tips:
If you use noConflict
, change $
to jQuery
in your code:
jQuery(document).ready(function() { /* ... */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With