3

I'm new to javascript, and I'm trying to run the following code:

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

script.js:

function popup() {
    alert("Hello World")
}

script.js is in the same directory as the html file, but when I push the button it doesn't execute the code.

3
  • your script.js file must not be included. Look at this jsfiddle.net/F2865 ... your code works fine. Commented Mar 14, 2011 at 17:47
  • Use your browser's Javascript console (shift-ctrl-J in Firefox, for instance) to see if there's any errors. Commented Mar 14, 2011 at 17:50
  • I literally was just fighting with this same thing for a while. My problem was a simple typo. Other than that your code looks good. I just checked the semicolon problems. I took all mine out and it still works great (I'm still putting them back though). Make sure your files are in the same directory. That way the <script> will link correctly. Also open your JavaScript console on your browser. This helped me some in tracing the html/js. Commented Jan 26, 2015 at 20:08

5 Answers 5

2

both the alert() in your function and your call to the function needs to be closed with a ;. So,

<html>
<head>
<script type="text/javascript" src="script.js" />
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

script.js:

function popup() {
    alert("Hello World");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Not the function. The call to the function and the call to alert.
@Fantius, yeah, that's what I meant
Semicolons are not required (though they are, of course, recommended).
@Tomalak aka Jim Morrison, depends on the browser
@Tomalak, I feel you. Wish it did, but I'd answer a whole lot less SO questions
0
<html>
<head>
    <script type="text/javascript" src="button.js">
    </script>
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

you just missed a semicolon.

2 Comments

I am not a javascript guru, I just use it to do some stuff on my websites , so please explain .
na: Explain what? They're not required; simple as that. (That's not to say, though, that they're not recommended. They are. They just have nothing to do with the OP's problem.)
0
<html>
<head>
    <script type="text/javascript">
        function popup() { alert('lol'); }
    </script>
</head>
<body>
    <input type="button" onclick="popup();" value="Click Me!" />
</body>
</html>

Like this that's works, check your path is correct.

Comments

0

semicolons are only needed if you want to combine code on one line

alert("Hello World");alert("Hello World");

I copied and pasted your exact code and it worked fine for me. The only explanations are you have an error in your script.js file with another function in it.
Or you have a funky character in the text file that the browser doesn't know what to do with.
Delete all spaces and tabs and try it again.

Comments

0

Your exact code works on my machine, so I think you may have a wrong file name. If you are using Windows, remember Windows hides the extension of files, so you may end up with a file named script.js.txt instead of script.js.
Check the exact file name.
Also, try ggregoire's code to see if you have a pop-up blocker active or something like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.