3
858master
124d

I can't use JS libaries, and I don't know why. The picture shows my code (in an HTML file). test.js is a file and test() is a function that makes an alert. It doesn't do anything, no error messages whatsoever. But if I try calling test() from the console it is not defined. Can someone explain why? Am I doing a stupid syntax mistake or have I got everything wrong?

Comments
  • 6
    You're defining where the script should be found, and contents. Don't do both, do one or the other
  • 0
    @jschmold So I don't put anything inside the script tag and just let test.js execute by itself?
  • 4
    Either write your script inside a separate text.js file or omit the src="" part
  • 3
    When you include a script file, it's literally a script. But it also defines things. So when you create functions and variables, they stick around. So, if you include a file called "donkey.js", and it had a function called "kongRoar()", you can call kongRoar() in any script file after the inclusion of "donkey.js"

    So, when you include a file, it runs through and performs the things in the file, even if the things it performs is to declare stuff.

    But the DOM assumes if you have a src attribute, you want to only use the file and ignores contents. If there's no src attribute, it assumes you have contents
  • 0
    @jschmold So if I remove the code inside the <script> element and only define its src and type, will it run test.js?
  • 1
  • 1
    @jschmold Thank you for your help!
  • 0
    @858master By the way, how do I do if I want to let test.js have functions that I call in test.html if the things in the script tags don't get executed?
  • 0
    @858master simple you just copy the code that you wanted to have between the tags at the end of the file test.js . So I guess "test()". There are ofcourse better ways to this like events.
  • 1
    <head>
    <script src...> This file includes the function test.
    </head>
    <body>
    <script>test();</script>
    </body>
  • 0
    @Synti Oh so I just load test.js and can then use it in another script tag?!
  • 1
    @858master yep, thats right!
  • 2
    @858master yes but the best practise is to put it all in one file. The whole point of putting it in another file is so you dont have random js between your html.

    ---- file test.js ----
    Function baah(){
    Alert("baah");
    }
    baah()
    ---- end -----
  • 1
    @Synti @Jifuna I thought more about library stuff so that you don’t have to rewrite code, so I basically want to use the js file to define functions and variables. Although I guess I have all information I need for that now.
  • 1
    Baby steps @Jifuna
    Baby steps... 🤣

    If your script is in the <head> and you want to modify some node right away, it won't work.
    You need to assign an event handler.
  • 1
    @Synti Yeah, I guess you're right. He is still learning ofcourse.
  • 1
    @Jifuna well im not good at teaching anything and my approach could very well be wrong.
Your Job Suck?
Get a Better Job
Add Comment