php - How can I get $.getScript to work correctly?

934

I can see that the $.getScript bit of jquery would be handy once it's working, but it's the last little bit I'm having problems with. I'm suspecting the problem is caused by the way I'm loading JS files. How do I get this to work? My index.html is actually index.php and has very little between the body tags. I'm using jquery 1.7.1

<body id="btLive">
     <script>
          buildInterfaces();
     </script>
</body>

buildInterfaces() resides inside a document named layoutLoader.js. In that file, I have the following:

$(document).ready(function() {
     $('#hToggle').click(function() {
          aaa = "Not working.";
          $.getScript('test.js', function() {
               alert(aaa);
          });
          toggleHoverPanel();
     });
});

As my test, test.js is in the same folder as layoutLoader.js and contains only the following:

aaa = "Successful Load."

When I click on the button hToggle, toggleHoverPanel takes place and I get no alert. If I place an alert after aaa="Not working."; I get the alert I expect, telling me it's not working. I have a number of items that I need to use this for, but for the life of me, I cannot get it to work. Help?

164

Answer

Solution:

With high probability test.js file could not by found. Add this piece to your code and try again:

$(document).ajaxError(function(event, request, settings){
  alert('error loading: ' + request.status);
});

People are also looking for solutions to the problem: Better way of storing "messages" data in MySQL using PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.