Home>

I am trying to implement a function to smoothly scroll links within a page using jQuery on the current web page, but I cannot load an external js file. What should I do?

  • When I checked with the developer tool, it seemed that the jQuery CDN was being read, but the js file was not being read.
  • Don't forget to add a # on the link.
  • There is no error in the jQuery code itself.
<head>
<link rel = "stylesheet" href = "css/resetstylesheet.css">
<link rel = "stylesheet" href = "css/stylesheet.css">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type = "text/javascript" src = "js/script.js"></script>
</head>


I tried the following two items, but they were not loaded.

<script type = "text/javascript" src = "/ js/script.js"></script>
<script type = "text/javascript" src = "./ js/script.js"></script>

script.js is in the js file.

  

This is jQuery with smooth scrolling

$(function () {
    $('a [href ^ = "#"]'). click (function () {
      var speed = 500;
      var href = $(this) .attr ("href");
      var target = $(href == "#" || href == ""? 'html': href);
      var position = target.offset (). top;
      $('body, html'). animate ({scrollTop: position}, speed, "swing");
      return false;
    });
  });
  • Answer # 1

    Is there a full-width space between

    andtype = "text/javascript"?

  • Answer # 2

    When there seems to be a problem, first check for minor mistakes, and leave it to others as much as possible
    For example, in this site, if you put only a strange part with a star, it will be fixed automatically, or you can find other check tool web services