最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - javascript only works in <script> tags, not js file - Stack Overflow

programmeradmin1浏览0评论

I'm making a clickable dropdown menu. But when I run the javascript in its own js file the js doesn't work. However, when I put the javascript in tags in my html document, the menu works perfectly fine. Does anyone know how to make the javascript work in its own js file?

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

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
};

I'm making a clickable dropdown menu. But when I run the javascript in its own js file the js doesn't work. However, when I put the javascript in tags in my html document, the menu works perfectly fine. Does anyone know how to make the javascript work in its own js file?

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

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
};
Share Improve this question edited Jan 1, 2017 at 7:14 user663031 asked Jan 1, 2017 at 0:05 COOKIECOOKIE 3944 silver badges13 bronze badges 4
  • html code? you should call that javascript after your html is processed. body onload="init();" or similar would due the trick. – washcloth Commented Jan 1, 2017 at 0:08
  • The issue is probably how you are including the script in your file - can you include the html where your <script> tag references the javascript file? – willoller Commented Jan 1, 2017 at 0:11
  • 2 What is the error when it does not run? – epascarello Commented Jan 1, 2017 at 0:11
  • @Daniek you assume much. – willoller Commented Jan 1, 2017 at 0:14
Add a ment  | 

3 Answers 3

Reset to default 7

Try

document.addEventListener('DOMContentLoaded', function() {
  // do stuff here

}, false);

You need to create a JS file called script.js and then add this to HTML document before closing body tag:

<script src="script.js"></script>

I assume you are not including the script correctly. Check your console for errors.

Also, if you are including script in the head section, it could be loaded before the HTML, therefore the selectors possibly could not be found. Including script at the end of the HTML document (before closing body tag) would assure the document is loaded before script is executed.

In order to execute JavaScript from an external file, you will need to include the file in the HTML file using something like the following:

<head>
        <script type="text/javascript" src="location/of/file/filename.js"></script>
</head>

From there, make the HTML element you are using call the JavaScript function you are wishing to execute.

Example:

<button onclick="functionName()">Click!</button>

This will then go to the function you are calling in the JavaScript file and execute the body of the function.

发布评论

评论列表(0)

  1. 暂无评论