1

I cannot get this simple script to run and been staring at it for awhile now and don't see any syntax errors. Simply want the alert to show on click.

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type='text/javascript'>

$(document).ready(function() {
    $("#authenticate_button").click(function() {
        alert("click");
    });
});

</script>

</head>
<body>
    <input id="code" type="text"> 
    <button id='authenticate_button'>Authenticate</button>
</body>
</html>
5
  • 4
    Are you opening the file in http:// ? If you open it in file:// it can't work. Commented Apr 17, 2013 at 18:57
  • what does the console in the browser say? Commented Apr 17, 2013 at 18:57
  • 1
    This isn't running in a local file is it? Commented Apr 17, 2013 at 18:57
  • It is a local file, why does that not work? Commented Apr 17, 2013 at 18:58
  • Tried this in jsfiddle jsfiddle.net/TeuBp and i can see the alert. Commented Apr 17, 2013 at 19:01

2 Answers 2

6

You're opening the file with your web browser directly. // is shorthand for your current protocol (which is file://), so jQuery isn't loading from Google's CDN.

You need to explicitly specify the protocol by adding http: before // in this line:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

I'd also add a doctype:

<!DOCTYPE html>
Sign up to request clarification or add additional context in comments.

Comments

0

You can also try out with this:

Using jQuery's CDN provided by MediaTemple

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

Using Microsoft's CDN

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>

as an alternative to jQuery js file.

I would suggest to go for this:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>  
<script>  
     // Fallback to loading jQuery from a local path if the CDN is unavailable  
     (window.jQuery || document.write('<script src="/scripts/jquery-1.9.0.min.js"><\/script>'));  
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.