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?
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.
<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_
Source: Stackoverflow.com