[jquery] $(document).ready not Working

I am trying to run jQuery and WebMethods with ASP.NET; I have added a ScriptManager to the master page, and a content on the content page

<asp:Content ID="ch" ContentPlaceHolderID="cHead" runat="server">
    <script language="javascript" type="text/javascript">
       $(document).ready(function () {
          alert("hi");
       });
    </script>
</asp:Content>

However this never fires, what am I missing?

This question is related to jquery asp.net ajax

The answer is


I had a similar problem, but I got it solved this way:

// wait until DOM is loaded
$(document).ready(function(){

    console.log("DOM is ready");

    // wait until window is loaded (images, links etc...)
    window.onload = function() {

        console.log("window is loaded");
        var your_html_element = document.getElementById("your_html_element_ID"),

    };
});

This will happen if the host page is HTTPS and the included javascript source path is HTTP. The two protocols must be the same, HTTPS. The tell tail sign would be to check under Firebug and notice that the JS is "denied access".


function pageLoad() {
    console.log('pageLoad');
    $(document).ready(function () {
        alert("hi");
    });
};

its the ScriptManager ajax making the problem use pageLoad() instead


I had copy pasted my inline js from some other .php project, inside that block of code there was some php code outputting some value, now since the variable wasn't defined in my new file, it was producing the typical php undefined warning/error, and because of that the js code was being messed up, and wasn't responding to any event, even alert("xyz"); would fail silently!! Although the erronous line was way near the end of the file, still the js would just die that too,

without any errors!!! >:(

Now one thing confusing is that debugger console/output gave no hint/error/warning whatsoever, the js was dying silently.

So try checking if you have php inline coded with the js, and see if it is outputting any error. Once removed/sorted your js should work fine.


Instead of using this:

$(document).ready(function() { /* your code */ });

Use this:

jQuery(function($) { /* your code */ })(jQuery);

It is more concise and does the same thing, it also doesn't depend on the $ variable to be the jQuery object.


One possibility when ready stops working is that you have javascript code somewhere that is throwing an exception in a $(document).ready(...) or $(...) call, which is stopping processing of the rest of the ready blocks. Identify a single page on which this is occurring and examine it for possible errors in other places.


One possibility, take a look:

<script language="javascript" type="text/javascript" src="./jquery-3.1.1.slim.min.js" />

vs

<script language="javascript" type="text/javascript" src="./jquery-3.1.1.slim.min.js"></script>

The first method is actually wrong, however the browser does not complain at all. You need to be sure that you are indeed closing the javascript tag in the proper way.


  • Check for your script has to be write or loaded after jQuery link.

_x000D_
_x000D_
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>_x000D_
_x000D_
//after link >> write codes..._x000D_
_x000D_
<script>_x000D_
    $(document).ready(function(){_x000D_
      //your code_x000D_
    })(jQuery);_x000D_
</script>
_x000D_
_x000D_
_x000D_


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios