Shaun Mccran

My digital playground

04
A
U
G
2008

JavaScript Confirm function

Recently I've been using the JavaScript confirm method to confirm if a user has clicked on the right thing or not. A handy 'catch-and-verify' fail safe.

I was doing it like this (the old fashioned way):

view plain print about
1<skript type="text/javascript">
2function confirm()
3{
4var r=confirm("Press a button");
5if (r==true)
6 {
7 document.write("You pressed OK!");
8 }
9else
10 {
11 document.write("You pressed Cancel!");
12 }
13}
14</script>
15</head>
16<body>
17
18<input type="button" onclick="confirm()" value="Display a confirm box" />

I've since discovered that you can do this in a much easier shorthand kind of way:

view plain print about
1<a href="#myself#app.delete&id=#id#" onclick="return confirm('Are you sure you want to delete this?');">

this performs the same confirm alert, and if true, sends the user on to the url in the hyperlink. Much shorter!

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top