0

Im working with sublime text and when ever i run the code it gives me "ReferenceError: document is not defined" can someone help?

here is the code:

const characters =   ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "{", "[", "}", "]", ",", "|", ":", ";", "<", ">", ".", "?",
  "/"
];

let boxoneEl = document.getElementById("boxOne-el")

let boxtwoEl = document.getElementById("boxTwo-el")


let Length = 15

function getPasswordNumbers() {
  let randomIndexOne = Math.floor(Math.random() * characters.length)
  return characters[randomIndexOne]
}


function generatePassword() {
  let password1 = ""
  let password2 = ""
  for (let i = 0; i < Length; i++) {
    password1 += getPasswordNumbers()
    password2 += getPasswordNumbers()
  }
  boxoneEl.textContent = password1
  boxtwoEl.textContent = password2
}
<html>

<head>
  <link rel="stylesheet" href="PasswordGen.css">
</head>

<body>
  <button onclick="generatePassword()"">Generate passwords</button>
  <h1> Generate a</h1>
  <h2>random password</h2>
  <h3>Never use an insecure password again.</h3>
  <h4 id="boxOne-el"></h4>
  <h5 id="boxTwo-el"></h5>
  <script src="PasswordGen.js"></script>
</body>

</html>

8
  • 1
    I get a different error. Get rid of the spaces at the end of the id attributes. Commented Jul 5, 2022 at 21:19
  • @Barmar Those spaces weren't there in the initial revision. The excessive double quote in <button onclick="generatePassword()"" > probably broke the code beautifier. Commented Jul 5, 2022 at 21:23
  • @Ivar i got rid of it and it still doesnt work Commented Jul 5, 2022 at 21:25
  • 1
    @RAlN4 Your error gives me reason to believe that you are executing the JavaScript via NodeJS or something similar. It isn't ran by the browser (or at least as part of the web page) as it should. Commented Jul 5, 2022 at 21:26
  • 1
    Seems to be working now. Commented Jul 5, 2022 at 21:27

1 Answer 1

2

Your question isn't directly related to Sublime Text because Sublime doesn't execute any code you might write (except if you give it instructions on how to do so, see below), and it doesn't interpret JavaScript at all; it's just a text editor.

That said, by "run your code" do you mean you've created a sublime-build file that executes your JavaScript program for you, say by using node (or failing that, followed a tutorial somewhere about "running" JavaScript in Sublime that included instructions like that)?

If the answer to that question is yes, then your solution is: don't. NodeJS is a server side implementation of JavaScript and doesn't know about document, which is a client-only thing.

To "Run" your code in this instance, you load the HTML file into your browser and allow it to run there, where document exists.

Sign up to request clarification or add additional context in comments.

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.