admin管理员组

文章数量:1335813

My friend done this below coding for custom control

<a href="javascript:__doPostBack('id','msg');">click</a>

now i want to show confirm dialog box while click this anchor link.

Is it possible?. i want to write script as inline.

My friend done this below coding for custom control

<a href="javascript:__doPostBack('id','msg');">click</a>

now i want to show confirm dialog box while click this anchor link.

Is it possible?. i want to write script as inline.

Share Improve this question asked Jun 21, 2012 at 9:24 YogeshWaranYogeshWaran 2,2814 gold badges24 silver badges32 bronze badges 4
  • Create a function (e.g. confirm()) where you do your according stuff and call it from __doPostBack(). – Christoph Commented Jun 21, 2012 at 9:27
  • @Christoph There already is a confirm function in javascript. – Denys Séguret Commented Jun 21, 2012 at 9:28
  • @dystroy If you want to use the standard ugly builtin confirm-dialog, yes. – Christoph Commented Jun 21, 2012 at 9:29
  • Using elegant confirm dialog is more work and requires some javascript/html/css knowledge. – Denys Séguret Commented Jun 21, 2012 at 9:31
Add a ment  | 

3 Answers 3

Reset to default 3

Do this :

<a href="javascript:if (window.confirm('Really?')){__doPostBack('id','msg');};">click</a>

But at some point, you'd want to stop using only inline code and have a look at other clearer ways to add javascript in your code.

You may use a script block like this in the HEAD of your HTML file :

<script>
    function doOnClick(){
       if (window.confirm('Really?')){
           __doPostBack('id','msg');
       };
    }
</script>

And then your link bees

<a href="javascript:doOnClick();">click</a>

Of course, this doesn't feel much simpler with only one function but it helps you put all your functions in the same place and make lighter and clearer html.


An alternative would be to use jQuery, so that you may totally avoid putting javascript in the html part.

The html is then

<a id=myLink>click</a>

And your script, now at the end of the body, is this one :

<script>
   $(document).ready(function(){
       $('#myLink').click(function(){
           if (window.confirm('Really?')){
               __doPostBack('id','msg');
           };
       });

       // other codes will e here
   });
</script>

You're not at all required to code it this way now, as you only have a very light function, but if your code grows I suggest you start considering it and look at the jquery tutorials.

Of course. Here is a small snippet, not elegant but it works...

<a href="javascript:if(confirm('Do you really want to post?')) {__doPostBack('id','msg');};">click</a>

I actually had to look this up because I haven't used confirm, alert and prompt in a very long time.

confirm returns true/false depening on what the user selected (OK/Cancel, respectively).

So your resulting code would be

<a href="javascript:if (confirm('Are you sure?')) __doPostBack('id','msg');">click</a>

本文标签: javascriptConfirm dialog box with anchor tagStack Overflow