Skip to content Skip to sidebar Skip to footer

Browser Doesn't Recognize Javascript / Jquery Code

I try run the following code in Firefox 27.0.1 and Chrome 30.0.1599.114 on a machine with Kubuntu Linux, and nothing happens. The html page is part from a web application based on

Solution 1:

You don't say what your "errors" are, but my guess is you've left out the document ready handler, so by the time your selector is run, the elements are not ready in the DOM yet.

$(function() {
  $('a').click(function() {
      alert("clicou em um link");
  });
});

Solution 2:

You are selecting all the a elements that exist at the time the script runs (all zero of them since the script runs in the head and all the a elements exist in the body) and doing stuff to them.

Move the script to just before </body> or create a function and run it on DOM ready.

jQuery(function () {
    $('a').click(function() {
        alert("clicou em um link");
    });
});

… then there will be actual a elements you can manipulate.

Solution 3:

You need to wrap your code inside DOM ready handler $(document).ready(function() {....}) or shorter form $(function() {.... }) to make sure all of your elements are properly added to the DOM before executing your jQuery code.

$(function() {
    $('a').click(function() {
        alert("clicou em um link");
    });
});

Post a Comment for "Browser Doesn't Recognize Javascript / Jquery Code"