Skip to content Skip to sidebar Skip to footer

Why Isn't This Very Simple JQuery Working?

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 s

Solution 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="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.


Solution 2:

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?


Post a Comment for "Why Isn't This Very Simple JQuery Working?"