views:

211

answers:

4

I am following a JavaScript tutorial on the W3Schools website and I have the following code:

<html>

<head>

<title>Hello!</title>

</head>

<body>

<script type="text/javascript">
function confirmShow
{
    var r = confirm("Press one...")
    if (r == true)
    {
        alert("Button pressed == OK")
    }

    if (r == false)
    {
        alert("Button pressed == Cancel")
    }
}
</script>
<input type="button" onclick="confirmShow()" value="Show Confirm Box" />
</body>



</html>

and whenever I preview it in Coda or in Safari the alert never shows up.

Thanks in advance!

A: 

I don't know if this is your problem, but your button is outside the <body> tag. That might cause you some trouble...

Also one would usually put a script like this in the <head> element. Just FYI.

Robusto
Oh, just noticed that. I put the button back in the body tag and it still isn't working.
Mark Szymanski
+3  A: 

"function confirmShow" => "function confirmShow()"

Firebug is good for js debugging, try it. Safari has options too, AFAIK.

Nikita Rybak
Ok, thanks. I don't know why I didn't see this :P
Mark Szymanski
Lol I didn't even notice he'd left off the parens ...
Robusto
A: 

1) w3schools is filled with errors and omissions. Better tutorials can be found at howtocreate.co.uk

2) You have no DOCTYPE declaration, and you're using XHTML syntax.

2.1) IE doesn't support true, see webdevout.net/articles/beware-of-xhtml for more information 3) You need to encapsulate the within a element as well as another block-level element as per the specification

See below for a proper HTML5 document. Notice the location and syntax

<!DOCTYPE html>
<html>
<head>
    <title>Hello!</title>
<script>
function confirmBox() {
    var ret = confirm('Some Text');
/*
Note the 3 equal signs. This is a strict comparison operator, to check both the 'value' as well as the type. see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators for more
*/
    if(ret === true) { 
        alert('Alert box for "Okay" value');
    }
    else if(ret === false) {
        alert('Alert box for "Cancel" value');
    }
}
window.onload = function() {
    // Execute the confirmBox function once the 'button' is pressed.
    document.getElementById('confirmBox').onclick = confirmBox;
}
</script>
</head>
<body>


<form>
<p>
<input type="button" id='confirmBox' value="Show Confirm Box">
</p>
</form>


</body>
</html>
boogyman
Ok, I didn't think I was using XHTML Syntax as the tutorials are on HTML, not XHTML. Hmm...
Mark Szymanski
+1  A: 

function confirmShow {

function confirmShow() { ?

j2me