Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can't find variable: function name in external js file

i have a problem with my website. I have couple of functions in java script that works in <body> <script> js code </script></body> but when i link external js file with exactly the same code functions that are called by onclick"function name" attribute stop working and I get error can’t find variable: function name also it seems like it can’t find some of ids for variables because i can’t log them. this is my code

function onload() {
  /* leaderboard variable */
  let x = document.getElementById('boardw');
  /* help popup variable*/
  let y = document.getElementById('helpw');
  /* settings popup variable*/
  let z = document.getElementById('setw');
  /* help button variable*/
  let a = document.getElementById('help');
  /* dropdown container variable*/
  let dropdown = document.getElementById('dropdown');
  /* footer popup display none*/
  document.getElementById('card').style = "display: none;";

  /* variable test */
  console.log(x);

  /* show footer popup */
  function showCard() {
    document.getElementById('card').style = "display: block;";
    document.getElementById('footer').style = "display: none;";
  }
  /* hide footer popup */
  function hide() {
    document.getElementById('card').style = "display: none;";
    document.getElementById('footer').style = "display: block;";
  }

  /* choose time in dropdown function */
  function show(anything) {

    document.getElementById('txt').value = anything;
  }

  /* show options in dropdown */
  dropdown.onclick = function() {
    dropdown.classList.toggle('active');
  }
  /* show leaderboard function*/
  function menu1() {

    x.classList.toggle('active');
  }
  /* show help popup function*/
  function menu2() {

    y.classList.toggle('active');
    a.classList.toggle('active');

  }
  /* show settings function*/
  function menu3() {

    z.classList.toggle('active');
  }

  /* hide popups function*/
  function remove() {
    y.classList.remove('active');
    z.classList.remove('active');
    x.classList.remove('active');
    dropdown.classList.remove('active');

  }
}
<body id="bd" style="" onload="onload()">

  <script src="script.js" charset="utf-8"></script>


  <!-- dropdown select time window  -->
  <div class="dropdown" id="dropdown" onclick="">
    <!-- dropdown textbox with chosen informations -->
    <input type="text" class="textbox" id="txt" value="" placeholder="     select the test duration" readonly>
    <!-- options for dropdown select -->
    <div class="option">
      <div onclick="show('   1 minute')">1 minute</div>
      <div onclick="show('   2 minutes')">2 minutes</div>
      <div onclick="show('   3 minutes')">3 minutes</div>
      <div onclick="show('   5 minutes')">5 minutes</div>
      <div onclick="show('   10 minutes')">10 minutes</div>
    </div>
  </div>


  <!-- checkboxes for charset in game -->
  <div id="charset">
    <!-- normal letters check -->
    <div>
      <label for="cka">a</label>
      <input type="checkbox" id="cka" class="ck">
    </div>
    <!-- capital letters check -->
    <div>
      <label for="ckB">A</label>
      <input type="checkbox" id="ckB" class="ck">
    </div>
    <!-- numbers check -->
    <div>
      <label for="ck1">1</label>
      <input type="checkbox" id="ck1" class="ck">
    </div>
    <!-- special characters check -->
    <div>
      <label for="ck>">#</label>
      <input type="checkbox" id="ck" class="ck">
    </div>

  </div>


  <!-- about popup -->
  <footer onclick="remove()">
    <!-- show popup btn -->
    <button id="footer" onclick="showCard();" style="">i</button>
    <!-- popup container -->
    <div id="card" class="card" style="">
      <!-- close popup btn -->
      <button id="close" onclick="hide()">x</button>
    </div>

  </footer>

  <!-- menu -->
  <menu>
    <!-- leaderboard popup -->
    <button class="menu" id="board" onclick="menu1()">L</button>
    <div id="boardw" style="" class="menuw">
    </div>

    <!-- help popup -->
    <button class="menu" id="help" onclick="menu2()">?</button>
    <div id="helpw" style="" class="menuw">
    </div>

    <!-- settings pop -->
    <button class="menu" id="settings" onclick="menu3()">S</button>
    <div id="setw" style="" class="menuw">
    </div>

  </menu>

  <!-- start game btn -->
  <div id="gma">
    <a href="#"><button id="start">Start</button></a>
  </div>

  <!-- frame for higher resolution screen-->
  <div class="h"> </div>



</body>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You wrapped your functions in function onload() { ... } so the inner functions can’t be reached from HTML.

  1. Remove this wrapper.
  2. Add defer attribute to the script
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading