jwzumwalt
jwzumwalt

Reputation: 201

javascript global page vars - is there such a thing?

I am attempting to create a HTML template using Firefox and would like certain variables easily accessible at the top of a page. I don't seem to be able to get this to work. Is it even possible?

For the code given below, I get...

undefined
copyright undefined

For example

<html>
<head>
  <script>
    var year = getFullYear() ;
    var pgTitle = "Page Title";
  </script>
</head

<body>
  <script>document.write("<h1>"+pgTitle+"</h1>");</script>
<footer>
  <script>document.write("copyright "+year);</script>
</footer>
</body>
</html>

Upvotes: 1

Views: 80

Answers (2)

micronyks
micronyks

Reputation: 55443

<html>
<head>
  <script>
    var year = new Date().getFullYear();
    var pgTitle = "Page Title";
  </script>
</head

<body>
  <script>document.write(`<h1>${pgTitle}</h1>`);</script>
<footer>
  <script>document.write("copyright "+ year);</script>
</footer>
</body>
</html>

Upvotes: 2

Unmitigated
Unmitigated

Reputation: 89404

You haven't defined getFullYear anywhere. You probably wanted new Date().getFullYear().

<html>
<head>
  <script>
    var year = new Date().getFullYear();
    var pgTitle = "Page Title";
  </script>
</head>
<body>
  <script>document.write("<h1>"+pgTitle+"</h1>");</script>
<footer>
  <script>document.write("copyright "+year);</script>
</footer>
</body>
</html>

Upvotes: 2

Related Questions