Reputation: 341
*<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
Upvotes: 4
Views: 36909
Reputation: 1
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.
Upvotes: 0
Reputation: 42099
confirm()
and it's in an infinite loop*
at the beginning and end of the documentwindow.confirm
</div>
in the <body>
<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>
Upvotes: 9
Reputation: 150010
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'.)
Upvotes: 2
Reputation: 160171
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.
Upvotes: 1
Reputation: 104760
First off, you overwrote window.confirm... Then you call the new confirm, forever, or until there is too much recursion...
Upvotes: 2