Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No alert message when document.ready - jQuery [closed]

Tags:

jquery

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?

like image 626
red23jordan Avatar asked Jan 02 '12 12:01

red23jordan


People also ask

Why jQuery document ready not working?

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.

What does $( document .ready function () mean?

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.

Is jQuery document ready deprecated?

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.

What is difference between $( function () and document Ready?

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.


2 Answers

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>
like image 102
Dennis Traub Avatar answered Sep 28 '22 09:09

Dennis Traub


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:

  • Look at your browser's JavaScript console for errors. Most likely there's a syntax error elsewhere on the page that's preventing the code you've quoted from ever being run.
  • Make sure you've loaded jQuery correctly.
  • If you use noConflict, change $ to jQuery in your code:

    jQuery(document).ready(function() { /* ... */ });
    
like image 45
T.J. Crowder Avatar answered Sep 28 '22 10:09

T.J. Crowder