Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, everyone. I tried to fix my site from hanging ( it just loads loads loads until a "Page Unresponsive" popup comes ) . I don't know what to do and... You know. I know, there might be infinite loops but- I can't find one! This is my code:

Code



HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Encrypted directory!</title>
    <link href="/files/style.css" rel="stylesheet">
    <style>
html {
  max-width: 100%;
  touch-action: pan-x pan-y;
}
    </style>
  </head>
  <body onload="main()">
    <p class="upper" style="margin: 0;">/files/>cd my</p>
    <center>
      <p>Encrypted directory!</p>
      <p>You need a code!</p>
      <p>[HEX] - viewer, [X]-exit</p>
      <p>Code: <span id="codeInput" class="upper"></span></p>
      <textarea style="display: none;" id="codeInputGetter"></textarea>
    </center>
    <!-- <script src="/files/script.js"></script> -->
    <script>
function main() {
  if ((document.readyState === "complete") || (document.readyState === "interactive")) {
    getCode()
  }
}

function getCode() {
  fakedInputGetter = document.getElementById("codeInputGetter")
  fakedInput = document.getElementById("codeInput")

  fakedInputGetter.focus()
  while ((fakedInputGetter.innerText.indexOf("\n") != -1) == false) {
    fakedInputGetter.focus()
    fakedInput.innerHTML = fakedInputGetter.innerText.replace("\n", "")
  }
}
    </script>
  </body>
</html>


What I have tried:

I tried to go to the Chrome lighthouse, and... That's how I knew that my site hung.
Posted
Updated 6-Feb-22 0:42am
v3

1 solution

I think the problem is in the following code:
JavaScript
while ((fakedInputGetter.innerText.indexOf("\n") != -1) == false) {
  fakedInputGetter.focus()
  fakedInput.innerHTML = fakedInputGetter.innerText.replace("\n", "")
}

If there is no newline character found by the call to indexOf, then the result will be -1. You then compare that result (for negative equality) with -1. But the boolean result of -1 != -1 is false. You then compare that result to be equal to false, which is true, so the while loop continues.
 
Share this answer
 
Comments
Tyrunt.new("Rix") 6-Feb-22 6:44am    
But... It focuses the textarea and then it waits until the textarea is finished, AKA, entering a new line. And then, if there's a new line, the loop ends. And see, it's/it should be not infinite at all!
Richard MacCutchan 6-Feb-22 6:51am    
Well, if I change that line to
      while ((fakedInputGetter.innerText.indexOf("\n") != -1) == true) {

it does not hang.
Tyrunt.new("Rix") 6-Feb-22 7:09am    
But it does not make the fake input typable anymore :(
Richard MacCutchan 6-Feb-22 7:14am    
There is some code missing from what you have posted as I did not get an input text area on my test.
Tyrunt.new("Rix") 6-Feb-22 7:17am    
Does the contenteditable attribute work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900