"Hello World" using external js file is not working!

Hello everyone,
Here is my html code:

<!DOCTYPE html>
<html lang="en">
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>--</title>
<script src="scripts/hello.js"></script>
</head> 
<body>
<button onclick="myFunction()">Click me</button>    
</body>
</html>

And here is my js external code located at “/scripts” folder:


    function myFunction() 
    {
     alert("Hello");
    }

Uploading the html file I get a button but when I click it nothing shows up.
Any suggestion?
Thanks

My first observation is that the script tag references a ‘scripts’ folder relative to the current folder.
According to your post, the ‘scripts’ folder is in the root (/scripts)

That may be your problem.

If your HTML file is in what we will call your ‘root’ folder, and the .js file is, indeed, in the ‘/scripts’ folder relative to where the HTML file is located, but it isn’t working, the first thing I would do is check the browser console for javascript error messages indicating what might the problem be.

If you are opening the document in a browser with “file:///” as the protocol, that could be a problem (although I doubt it.) However, if you have a webserver set up, try viewing the file through the webserver (IIS, Apache, etc.) If it’s on your local machine, you could try “http://localhost/filename.html” or “http://127.0.0.1/filename.html” in the browser.

HTH,

:slight_smile:

1 Like

Thank you WolfShade,
My js scripts worked fine on external files and suddenly it stopped. Files that worked before ceased to respond !
How do I get" “browser console for javascript error messages” ?
Thanks again !

Press F12 (or Ctrl+Shift+I) and open “Console” tab
See if there any error messages listed

1 Like

My first observation is that the script tag references a ‘scripts’ folder relative to the current folder.
According to your post, the ‘scripts’ folder is in the root (/scripts)

That may be your problem.
[/quote]

Thanks Parkin,
I’m not using a server. Just uploadung an HTML file (located on “d:\javascrupy” ) which calls a js file located at: “d:\javascript\scripts”. Must I do it on a web server (“apache”)? i

If you aren’t using a server where do you upload your files?

From “open a file (ctrl_o)” menu I select an HTML file. It might be that js scripts that worked for me in the past were built in the HTML file…
I’ll try using my apache web server…
Thanks

So what your browser console says?

HTMLl file is: “myjs.html”:

<html>
<head>
<script src="myjs.js"></script>
</head>
<body>
<input type="button" onclick="xxx()" value="Click Me!">
</body>
</html>

js file is: “myjs.js”
function xxx()
{
alert(“Hello World”)
}
Uploaded from"Open file" menu.
Error message says:
“ReferenceError: xxx is not defined”

Is your JS file in the same directory with the HTML file?
If it’s in scripts subdirectory then you should specify that:

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

Not uploaded, just opened. Upload is a term that means you transfer your file somewhere (i.e. upload to server)

I changed my js file location to the same folder where my HTML file is located. I’ll install Apache tomorrow and see if it works.
Thanks

[quote=“deotpit, post:10, topic:199582”]
function xxx(){alert(“Hello World”)}
[/quote]Here is the error message:

Make sure you save JS file in UTF-8 encoding

1 Like

This is how it eventually worked !
Thanks a lot megazoid !
Encoding UTF-8, that is…
Thank you so much ! You saved my external world :slight_smile:

gratz :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.