最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - confirm function is not working - Stack Overflow

programmeradmin4浏览0评论
*<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 .asp

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

Share Improve this question edited Oct 21, 2011 at 10:57 Erwin Brandstetter 657k157 gold badges1.1k silver badges1.3k bronze badges asked Oct 21, 2011 at 3:10 SiriusKoderSiriusKoder 3412 gold badges4 silver badges12 bronze badges 5
  • What's not working? Do you get any errors in the javascript console (shift-ctrl-J in Firefox/Chrome)? Does the page seem to reload when you click the button? You get the wrong alert? – Marc B Commented Oct 21, 2011 at 3:13
  • Is the * added intentionally? – Raptor Commented Oct 21, 2011 at 3:13
  • You missed a <div>, but this won't affect the JavaScript. – Raptor Commented Oct 21, 2011 at 3:14
  • you have the same function name. change your function name to something else. – Birey Commented Oct 21, 2011 at 3:14
  • Also, see this. – josh3736 Commented Oct 21, 2011 at 3:32
Add a comment  | 

5 Answers 5

Reset to default 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>

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

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'.)

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.

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.

发布评论

评论列表(0)

  1. 暂无评论