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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Verify the following steps.
- Did you include jquery
- Check for errors in firebug
Those things should fix the problem
Method 2
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.
Method 3
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.
Method 4
function pageLoad() {
console.log('pageLoad');
$(document).ready(function () {
alert("hi");
});
};
its the ScriptManager ajax making the problem
use pageLoad() instead
Method 5
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”.
Method 6
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.
Method 7
- 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>
Method 8
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.
Method 9
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"),
};
});
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0