$(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, wha开发者_开发技巧t am I missing?
Verify the following steps.
- Did you include jquery
- Check for errors in firebug
Those things should fix the problem
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.
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.
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>
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".
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.
function pageLoad() { console.log('pageLoad'); $(document).ready(function () { alert("hi"); }); };
its the ScriptManager ajax making the problem use pageLoad() instead
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.
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"),
};
});
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.
精彩评论