开发者

W3Schools tutorial example not working with Coda on Mac

I am fo开发者_运维百科llowing 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!


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

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


function confirmShow {

function confirmShow() { ?


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜