1

Today is my first day at a new job (a Front End Lead on a large website.) One of my tasks is to implement a button that fires an event when it's clicked. I had no clue, so after googling a bit, I came up with this:

<html>
<head>
<script type="text/javascript">
function popup(); 
{
    alert("Hello World") ==> alert("Hello World");
}
</script>
</head>
<body>

<input type="button" value="Click Me!" onclick="popup()"></input><br></br>

</html>    
</body>

The problem is, when I click my button, nothing happens.

EDIT Updated based on MICHEL's comments

Stack Guru
  • 873
  • 2
  • 8
  • 7
  • 4
    syntax error `function popup() { alert("Hello World"); }` – KJYe.Name Mar 28 '11 at 14:49
  • You may be interested in a series that I'm writing about jQuery's event system and discusses browser event models - http://forloop.co.uk/event-internals-in-jquery-part-one – Russ Cam Mar 28 '11 at 14:52
  • 6
    Front End Lead on a large website? o_O I greatly encourage you to learn jQuery as soon as possible. – JackWilson Mar 28 '11 at 14:54
  • 5
    I want to know where this guy works, maybe they will hire my daughter ;-) – Richard Friend Mar 28 '11 at 14:56
  • `alert("Hello World") ==> alert("Hello World");` is not a valid statement. – gen_Eric Mar 28 '11 at 18:55
  • @RichardFriend Hmm.... A large website with bare JavaScript in HTML and great colleagues.... difficult to get in and even more difficult to deliver work and be happy with yourself.... – 0xc0de Sep 27 '18 at 13:08
  • There's a semicolon after "function popup()" which makes popup a null function. The block that is after it is no longer the body of the function. – RufusVS Jun 16 '19 at 10:17

5 Answers5

6

remove the semi-colon:

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

that should work ^_^

see it working here: http://jsfiddle.net/maniator/fNeJh/

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • Where should I put the semicolon then? – Stack Guru Mar 28 '11 at 14:51
  • 1
    nowhere lol. do what i did above. there is no need for semicolons (you can use them if you want. put it after alert like: `alert("Hello World");`) – Naftali Mar 28 '11 at 14:51
  • 5
    I would advise those learning JavaScript to **not** leave off semicolons. – Pointy Mar 28 '11 at 14:54
  • 1
    @Pointy , true, as i said, they are not needed, but you can use them, just at the end of lines, not function creating – Naftali Mar 28 '11 at 14:54
  • 1
    I'm with @Pointy ;) Also, put a semicolon after a function expression: `var foo = function () {};`. Better yet, use [JSLint](http://www.jslint.com/). – johnhunter Mar 28 '11 at 17:11
3

Your HTML is invalid. Change

</html>    
</body>

to

</body>
</html>

Also, the <br> tag has no content, so you don't need a closing tag at all (for HTML) or you can write it as a self-closing tag: <br/>.

As far as the JavaScript error goes, Neal's answer is correct.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

This should work

<html>
<head>
</head>

<body>

<script type="text/javascript">
function popup() 
{
    alert("Hello World");
}
</script>

<input type="button" value="Click Me!" onclick="popup()"><br/><br/>

</body>
</html>
Scherbius.com
  • 3,396
  • 4
  • 24
  • 44
1

Remove semi-colon after

Bad :

function popup(); 

Good :

function popup()

You might want to add one after alert("Hello World") ==> alert("Hello World");

Michel
  • 265
  • 1
  • 8
0

function should not have a semi colon after its declaration ... A simple tutorial on javascript would give you a start on javascript...

  • 1
    This is a kind of simple mistake not always easy to see... even from more experienced ones... – Michel Mar 31 '11 at 09:07
  • It's a common practice to put a semicolon at the end of a function declaration because not doing so might have an undesired effect. See http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript . –  Jan 22 '13 at 23:52