user192936
user192936

Reputation:

$(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?

Upvotes: 32

Views: 148392

Answers (10)

MohammadSoori
MohammadSoori

Reputation: 2408

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

<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>

//after link >> write codes...

<script>
    $(document).ready(function(){
      //your code
    })(jQuery);
</script>

Upvotes: 3

Bbb
Bbb

Reputation: 649

I tried all solutions here and web searched for over an hour.

My issue turned out to be needing to clear the cache. I noticed that what I had in localintranet.js was not reflecting what I had typed. I checked this by (In Chrome) opening up dev tools with F12, clicking on the "Sources" menu option and hten drilling down to my scripts folder and the .js actual file name.

Upvotes: 0

Stormborn
Stormborn

Reputation: 11

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"),

    };
});

Upvotes: 0

agodinhost
agodinhost

Reputation: 391

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.

Upvotes: 0

Rom Mil
Rom Mil

Reputation: 19

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

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

Upvotes: 1

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13928

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.

Upvotes: 1

Dave Cousineau
Dave Cousineau

Reputation: 13158

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.

Upvotes: 35

Richard
Richard

Reputation: 11

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".

Upvotes: 1

Lonare
Lonare

Reputation: 4663

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.

Upvotes: 16

kobe
kobe

Reputation: 15835

Verify the following steps.

  1. Did you include jquery
  2. Check for errors in firebug

Those things should fix the problem

Upvotes: 37

Related Questions