1

I have created some htm pages with JavaScript code which I saved on my Windows desktop. The code runs fine after I confirm a prompt to run scripts or Active X, but as soon as I copy the file over to a network location (not a server) and try to open the file, JavaScript does not run any more (the prompt mentioned above does not appear). I did not have this problem with other code (including JavaScript) and I am not sure what the problem is - probably something around network safety settings?

Specifically, the following snippet seems to cause the problem:

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
  </div>
</div>

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
} 

.show {display:block;}

The button shows, but the dropdown menu does not show up when the file is saved on the network drive. So function myfunction() does not work apparently.

Joerg
  • 11
  • 2

1 Answers1

0

If you want to use JavaScript and CSS in your HTML, instead of including it as .js and .css, you should let the browser know what kind of code its dealing with.

<style>
.show {
  display: block;
}
</style>

 <div class="dropdown">
   <button onclick="myFunction()" class="dropbtn">Dropdown</button>
 <div id="myDropdown" class="dropdown-content"></div>
</div>

<script>
  function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
  }
</script>

So if you want to use JavaScript inside your .html file, always wrap it between the
<script> /* Your JavaScript */ </script> tag.

For CSS use the <style> /* Your CSS */ </style> tag.