user3004110
user3004110

Reputation: 969

How to have jQuery code execute after DOM is completely loaded

I am using asp.net MVC 5 and trying to get jQuery alert message when a form completely loads all its content. I have the following code that executes successfully and displays alert that time when a form has nothing displayed and totally blank, thanks

<script>
   alert("Registered Successfully");
</script>

Upvotes: 0

Views: 2540

Answers (3)

Talmid
Talmid

Reputation: 1391

Slight variation that is even shorter:

<script>
    $(function() {
        alert("Registered Successfully");
    });
</script>

Upvotes: 0

Amr Elgarhy
Amr Elgarhy

Reputation: 68902

You can do one of these:
- Put your script at the end of the page.
- Write your code in $(function() { });or $( document ).ready(function() { }
- Write your code in $(window).load() to wait for all content including images.
- Without jQuery:

window.onload = function WindowLoad(event) {
    alert("Page is loaded");
}

Upvotes: 2

Stan Shaw
Stan Shaw

Reputation: 3034

Wrap it in document.ready:

<script>
    $("document").ready(function(){
        alert("Registered Successfully");
    });
</script>

Or, use the shorthand:

<script>
    jQuery(function($) {
         alert("Registered Successfully");
    });
</script>

Upvotes: 2

Related Questions