Question

I've spent a couple of hours looking at this and my head's spinning. Can anyone tell me why this code isn't working? There are two complete files here: index.html, and test2.js, which are on a server on my local network.

I've tried this on FF, Chrome, and Opera on my desktop, and none of them do anything - clicking the button does exactly nothing. Firebug is showing that the XHR response is correct (the complete test2.js script), with status "200 Ok". I've tried all sorts of variations, but they all do the same thing. This one has no callback, but the alert in the loaded script isn't being executed. When I do have a callback, the debugger shows that it's not being run, and an alert in the callback isn't executed. Thanks.

index.html:

<html>
<head> 
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

  <script type="text/javascript">
  $(document).ready(function() {
     $("#submit").click(function(){
        $.getScript("test2.js");
     });
  });
  </script>
</head>
<body>
<button id="submit" type="button">Click Me!</button>
</body>
</html>

test2.js:

<script type="text/javascript">
  alert("Hello World!");
</script>
Was it helpful?

Solution

You do not use <script> tags in the .JS file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top