0

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="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    $(document).ready(function() 
    {
           alert("hello");
    });
</script>

And yet, it still does nothing.

  1. I've included JQuery
  2. I've triple checked my syntax
  3. No errors are reported in the console
  4. I looked through the other similar SO 'facepalm' questions, but none of their solutions work (see above)

What incredibly obvious thing am I missing?

bd33
  • 502
  • 1
  • 15
  • 30
  • 1
    [Pertinent](http://stackoverflow.com/a/19689613/621962). – canon Jan 31 '14 at 19:12
  • there is a good article from John Resig regarding this behaviour: http://ejohn.org/blog/degrading-script-tags/ – A. Wolff Jan 31 '14 at 19:16
  • This got closed faster than i could find a more appropriate duplicate, but I feel that this is more closely related to [a different question](http://stackoverflow.com/questions/3540581/what-if-script-tag-has-both-src-and-inline-script). – zzzzBov Jan 31 '14 at 19:19

2 Answers2

9

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="http://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 src attribute, depends on the value of the type attribute, but must match script content restrictions.
If there is a src attribute, the element must be either empty or contain only script documentation that also matches script content restrictions.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
3

It should be

<script type="text/javascript" src="http://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?

Community
  • 1
  • 1
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111