JQuery script does not work when viewing page on localhost

I am just starting to play on Mac for the first time, and I have created a very simple HTML page that uses jQuery to easily exchange text when the h1 tag is clicked.

When I do not view the page through the web server and simply open it directly in Safari (file: ///Applications/xampp/xamppfiles/htdocs/test/mypage.html), it works as expected. However, when I try to browse through Apache ( http: //localhost/test/mypage.html ), this will not work.

Here is the code :

<html>  
    <head>  
        <title>My Awesome Page</title>  
        <script type="text/javascript" src="jquery.js"></script>  
        <script type="text/javascript" charset="utf-8">  
            function sayHello()  
            {   $('#foo').text('Hi there!');  
            }  
        </script>  
    </head>  
    <body>  
        <h1 id="foo" onclick="sayHello()">Click me!</h1>  
    </body>  
</html>

Am I missing something on the Mac? Would not be setting up Apache from its client code. Correctly?

, , , XAMPP Apache MySQL. Apache PHP .

+3
1

Firebug . , , , - jquery.js - . Firebug , jquery , jQuery "".

Safari, Web Inspector , - Console.

, onclick, :

<script type="text/javascript" charset="utf-8">
  function sayHello()
  {
    $('#foo').text('Hi there!');  
  }
  //wait DOM loaded
  jQuery(function($){
    $('#foo').click(function(){
      sayHello();
    });  
  });
</script>

, js </body>, .

+3

Source: https://habr.com/ru/post/1730640/


All Articles