Jump to content

function uncaught referenceerror is not defined


klasss
Go to solution Solved by Ivo Vicente,

Recommended Posts

Quando vou a consola no browser tenho esse erro : 

 

 <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous" defer></script>
        <script src="https://cdn.socket.io/4.4.1/socket.io.min.js" integrity="sha384-fKnu0iswBIqkjxrhQCTZ7qlLHOFEgNkRmK2vaO/LbTZSXdJfAu6ewRBdwHPhBo/H" crossorigin="anonymous"></script>


        <script>
            $(function() {
                let ip_address = '127.0.0.1';
                let socket_port = '3000';
                let socket = io(ip_address + ':' + socket_port);

                let chatInput = $('#chatInput');

                chatInput.keypress(function(e) {
                    let message = $(this).html();
                    console.log(message);
                
                })

                
            });
        </script>


O erro vem da seguinte linha 
 <script>
            $(function() {

Alguem sabe o motivo de este erro acontecer? 

Obrigado ! 

Link to comment
Share on other sites

  • Solution

Viva, 

Estas a carregar a biblioteca com a opção defer o que faz com que só seja feito o seu download no fim da página indicar o estado ready

Mas antes da pagina estar pronta estas a tentar usar o Jquery que ainda não está no sistema.

Para resolver, remove esse atributo do tag script ficando

<script
	src="https://code.jquery.com/jquery-3.6.0.js"
    integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
    crossorigin="anonymous">

Mais detalhes interessantes: https://www.w3schools.com/tags/att_script_defer.asp

Feito é melhor que perfeito

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.