3

I'm working on asp.net and want to execute following javascript code. I'm using VS2010.

   <title></title>
<script type="text/javascript">
    function myClosure() {
        var canyousee = "here I'm ";
        return (function theClosure() {
            return { canyouseeIt: canyousee ? "yes" : "no" };
        });
    }
    var closure = myClosure();
    closure().canyouseeIt;
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myClosure();" />
    </div>

How do i execute function myClosure() on button click so that It gives me confirm i.e popup for yes or no ?

  1. Which code I need to put for confirm ?
  2. How can I execute it on Button Click ?

thanks

3
  • 1
    that's not alert, that's confirm Commented Feb 28, 2013 at 12:54
  • which code should I need to add for that ? Commented Feb 28, 2013 at 12:57
  • confirm('this is a yes no question') and it returns boolean value Commented Feb 28, 2013 at 12:58

4 Answers 4

4

I hope this can be of some help. I must confess that what you want to achieve is not clear to me.

<title></title>
<script type="text/javascript">
     myClosure=function() {
        var canyousee = "here I'm ";
        return (function () {
            return { canyouseeIt: function(){return confirm (canyousee)}};
        });
    }
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="(myClosure())().canyouseeIt()" />
    </div>
Sign up to request clarification or add additional context in comments.

Comments

4
<html>
<head>
<script type="text/javascript">
function myClosure(){
          var r=confirm("Press a button!")
        if (r==true)
        {
             alert("You pressed OK!")
             return true;
        }
        else
        {
              alert("You pressed Cancel!");
              return false;
        }

}
</script>
</head>

<body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myClosure();" />
</div>
</body>
</html>

Comments

4

use

onclientclick="javascript:return YourFunction();"

<script> 
function YourFunction() {
//Do your stuff

return true;
}
</script>

If you return true your asp.new onclick event will be called.. If you return false it wont be called..

Comments

1

as you call it, but prevent the post back by return false on

OnClientClick="myClosure();return false;"

I do not know nether understand the rest of your logic, but this is your issue right now.

2 Comments

not working for me :( in my logic i'm basically tried to work on javascript closure concept nothing else.
@ashuthinks When I say, your logic, I mean your steps you like to follow. Anyway, check for other javascript errors. This is the way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.