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.

Rene Terstegen
  • 7,911
  • 18
  • 52
  • 74
hhh3112
  • 2,167
  • 10
  • 36
  • 55
  • your `script.js` file must not be included. Look at this http://jsfiddle.net/F2865/ ... your code works fine. – Kyle Mar 14 '11 at 17:47
  • Use your browser's Javascript console (shift-ctrl-J in Firefox, for instance) to see if there's any errors. – Marc B Mar 14 '11 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 – Noah Herron Jan 26 '15 at 20:08

5 Answers5

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");
}
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
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.

Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
  • I am not a javascript guru, I just use it to do some stuff on my websites , so please explain . – Vamsi Krishna B Mar 14 '11 at 18:02
  • 2
    na: Explain what? They're not required; simple as that. (That's not to say, though, that they're not recommended. [They are](http://stackoverflow.com/questions/537632/should-i-use-semi-colons-in-javascript). They just have nothing to do with the OP's problem.) – Lightness Races in Orbit Mar 14 '11 at 18:05
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.

GG.
  • 21,083
  • 14
  • 84
  • 130
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.

The_asMan
  • 6,364
  • 4
  • 23
  • 34
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.

Pablo Grisafi
  • 5,039
  • 1
  • 19
  • 29