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>

Jack Bashford
44.3k11 gold badges56 silver badges84 bronze badges
asked Apr 19, 2019 at 19:33
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());
 }
answered Apr 19, 2019 at 19:38
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>

answered Apr 19, 2019 at 19:47

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>

Zeeshan Ahmad Khalil
8632 gold badges16 silver badges33 bronze badges
answered Apr 19, 2019 at 19:45

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>
Ehsan K. harchegani
3,1381 gold badge19 silver badges19 bronze badges
answered Apr 20, 2019 at 5:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.