4
*<html>
<head>
<title>practise</title>
<script type="text/javascript">
function confirm() {
    var r = confirm("Press the button");
    if (r == true) {
        alert("You are right");
    } else {
        alert("You are wrong");
    }
}

</script>
</head>

<body>
        <input type="button" name="submit" value="showtime" onclick="confirm()"/>
    </div>
</body>
</html>*

I want to know what's the problem. It is not working but it is the same as in http://www.w3schools.com/js/js_popup.asp

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
SiriusKoder
  • 341
  • 2
  • 4
  • 12

5 Answers5

9
  1. You are recursively calling confirm() and it's in an infinite loop
  2. You have a * at the beginning and end of the document
  3. As kennebec pointed out, you're overwriting window.confirm
  4. You have a hanging end </div> in the <body>

http://jsfiddle.net/cvyyL/

<html>
   <head>
      <title>practise</title>
      <script type="text/javascript">
         function show_confirm() {
            var r = confirm("Press the button");
            if (r == true) {
               alert("You are right");
            } else {
               alert("You are wrong");
            }
         }    
      </script>
   </head>
   <body>
      <input type="button" name="submit" value="showtime" onclick="show_confirm()"/>
   </body>
</html>
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • hey thanks. Do you know any better site than w3schools to learn javascript and jQuery. Please suggest. – SiriusKoder Oct 31 '11 at 03:53
  • Unlike many people, I think w3schools is great for what it is (a quick lookup to an attribute or element), but you should know its pitfalls. See http://www.w3fools.com for more information. – vol7ron Jul 07 '14 at 17:27
2

First off, you overwrote window.confirm... Then you call the new confirm, forever, or until there is too much recursion...

kennebec
  • 102,654
  • 32
  • 106
  • 127
2

Your function has the same name as the built-in window.confirm() function, so your function is replacing that one. Then within your function you call confirm() which means it is recursively calling itself.

It should work if you simply rename your function to something else. (E.g., the w3schools page you link to calls its function 'show_confirm'.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

You need to return the value from the confirm() function, and return that value in the onclick handler if you're trying to use it to prevent the submission.

IMO it's a really bad idea to name your function "confirm", though, since there's already a method called that, and you may recurse until the browser melts.

Also, saying something "doesn't work", without saying what it does and what you expect it to do, makes answering questions difficult.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

confirm() is a predefined function in javascript so you can't use it for the name of one of your functions. It is an error, rename the function and it will work properly.

Also remove the * from the start and the end.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43