0

The issue it that I can't get a simple JavaScript function to run with HTML. I get an error that says "Uncaught ReferenceError: getYear is not defined".Thanks!

$(document).ready(function () {
  function getYear(){
    document.write(new Date().getFullYear());
  }
});
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/script.js"></script>
</head>

<body>
    <footer>
        <p>&copy; <script> getYear()</script> | All rights reserved</p>
    </footer>
</body>
</html>

4
  • getYear function creates only after the document is ready. Move the function declaration outside the ready callback Commented Apr 19, 2019 at 19:35
  • You've declared your function inside the "ready" handler. That makes it unavailable to anything outside that scope. Commented Apr 19, 2019 at 19:35
  • That works! Thanks! Commented Apr 19, 2019 at 19:37
  • 1
    Why is document.write considered a “bad practice”? Commented Apr 19, 2019 at 19:41

4 Answers 4

2

I believe that's because you wrapped the function in your js file in a document.ready state so you're unable to access the function inside your HTML footer tag because of that.

removing the document.ready will solve the issue.

your new code should be

  function getYear(){
    document.write(new Date().getFullYear());
  }
Sign up to request clarification or add additional context in comments.

Comments

1

You have to remove ready function, because this function waits that all page onload, only use:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    	
  function getYear(){
    document.write(new Date().getFullYear());
  }

    </script>
</head>

<body>
    <footer>
        <p>&copy; <script> getYear()</script> | All rights reserved</p>
    </footer>
</body>
</html>

Comments

0

Try the following code;

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/script.js"></script>
</head>

<body>
    <footer>
        <p> &copy; <script>
                document.write(new Date().getFullYear());
            </script>| All rights reserved </p>
    </footer>
</body>

</html>

Comments

0

You would not need the

 $(document).ready(function () {...

Your new code should be like:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


<script>

function getYear(){ return  new Date().getFullYear();  }

</script>

</head>

<body>
    <footer>
        <p>&copy; <script> getYear()</script> | All rights reserved</p>
    </footer>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.