I was trying to pass the width of a page as a variable to C# code behind asp.net, and was only receiving empty strings. I narrowed down to the problem that the JQuery function is simply not firing. I changed it to the simplest code I could just to test if the function was doing anything:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
$(document).ready(function()
{
alert("hello");
});
</script>
And yet, it still does nothing.
- I’ve included JQuery
- I’ve triple checked my syntax
- No errors are reported in the console
- I looked through the other similar SO ‘facepalm’ questions, but none of their solutions work (see above)
What incredibly obvious thing 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
You can’t simultaneously set the [src] attribute and include contents for the <script> element.
If you need two scripts, you need to use two separate <script> elements.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
jQuery(function($) {
alert("hello");
});
</script>
As per the HTML5 spec on the <script> element:
If there is no
srcattribute, depends on the value of thetypeattribute, but must match script content restrictions.
If there is asrcattribute, the element must be either empty or contain only script documentation that also matches script content restrictions.
Method 2
It should be
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function()
{
alert("hello");
});
</script>
Here is a post that explains this clearly-
What if script tag has both “src” and inline script?
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