0

I have one script inside an HTML file. I want to make it as an external script bec I need it in more than one files.

When I move it to an external file, it started showing errors, some probably need an import of another js library that was included in html.

Here is a minimal script with errors line, where it is raising. How doe remove these.

RE-EDIT

The errors I put, these are shown by Brackets ide when I save the script file. In HTML, it is rendering the chart. There was a typo in path, that I corrected. Though the errors in the brackets ide are same still.

Errors;

'document' is not defined 
 myChart is defined but never used // I called script in html
'Chart' is not defined

External JS:

function loadChart (var)
          {
            var ctx = document.getElementById('myChart').getContext('2d');
            var myChart = new Chart(ctx, {
            . 
            .
            .
            .
            .

         }
XCeptable
  • 1,247
  • 6
  • 25
  • 49
  • 1
    are you perhaps placing your external JS before your reference to Chart.js? – cmprogram Nov 13 '18 at 11:16
  • From what you've posted it's impossible to say what the problem might be. In cases like this it's normally due to the load order changing. That would be the first thing to check, whether scripts' dependencies are present when they need them. – Mitya Nov 13 '18 at 11:17
  • Possible duplicate of [ReferenceError: document is not defined (in plain JavaScript)](https://stackoverflow.com/questions/24647839/referenceerror-document-is-not-defined-in-plain-javascript) – DontVoteMeDown Nov 13 '18 at 11:18
  • make sure you place a reference to external file in the same place your inline script was, thus preserving the correct order of files. – vikdotdev Nov 13 '18 at 11:18
  • Have you waited until the DOM is loaded? window.onload=function(){ // my javascript function here }; document is not defined until window onload – sferret Nov 13 '18 at 11:26
  • @cmprogram: that wouldn't make any difference bec these errors are raised by the script file when I saved it in brackets ide. – XCeptable Nov 13 '18 at 11:33
  • @Utkanos: may be I forget to add that these errors are caused by the brackets ide when I saved js file. Script dependencies are in HTML, I called it before Chart in HTML. – XCeptable Nov 13 '18 at 11:36
  • _“The errors I put, these are shown by Brackets ide when I save the script file.”_ - that sounds more like some code validation, than actual _run time_ errors. _“In HTML, the chart that js was supposed to display was not displayed. That means the script didn't loaded.”_ - no, it does not necessarily mean _that_. Instead of making such guesses, you should check for actual error message in the browser error console first of all. – misorude Nov 13 '18 at 11:58
  • @misorude: I did that & there is no error. though there was a path mistyped, I corrected it. The script is now working as before it was inside the html, rendering a chart. Though the errors that are in the ide that I put in OP are still existing, but script is working fine. – XCeptable Nov 13 '18 at 12:45

0 Answers0