Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts

    how to call function after confirm ok

    hi all

    i have two function each doing separate work.
    Code:
    <script language="javascript">
    function conf()
    {
    	confirm("do you want to exit");
    }
    function todo()
    {
    	alert("bye");
    	and some more code here
    }
    </script>
    i am calling function conf() in <body onunload="conf()"> which is giving a confirmation alert that provides two option ok and cancel.

    now i want that if the user clicks ok then my second function which is functiontodo() would be called on that press of ok, if he clicks cancel then that function should not be called.

    how is it possible.

    vineet

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vineet View Post
    hi all

    i have two function each doing separate work.
    Code:
    <script language="javascript">
    function conf()
    {
    	confirm("do you want to exit");
    }
    function todo()
    {
    	alert("bye");
    	and some more code here
    }
    </script>
    i am calling function conf() in <body onunload="conf()"> which is giving a confirmation alert that provides two option ok and cancel.

    now i want that if the user clicks ok then my second function which is functiontodo() would be called on that press of ok, if he clicks cancel then that function should not be called.

    how is it possible.

    vineet
    Code:
    <script type="text/javascript">
    function todo(){
    	alert("bye");
    	and some more code here
    }
    function conf(){
    	if(confirm("do you want to exit")){
                todo();
            }
    }
    </script>
    regards

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    HI OESXYL

    thanks for the reply. that is working fine.

    i would like to ask you one more question that i m using
    Code:
    <script language="javascript">
    window.onunload = function() 
    {  
     alert("bye");
    }
    </script>
    
    This code should give me alert only on windows unload means when i close browser tab then why is this giving me alert everytime i refresh the page.
    
    vineet

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vineet View Post
    HI OESXYL

    thanks for the reply. that is working fine.

    i would like to ask you one more question that i m using
    Code:
    <script language="javascript">
    window.onunload = function() 
    {  
     alert("bye");
    }
    </script>
    
    This code should give me alert only on windows unload means when i close browser tab then why is this giving me alert everytime i refresh the page.
    
    vineet
    because each time you reload the page is fire onload.

    off topic, use type="text/javascript" or other type instead of language attribute.

    regards

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    hi OESXYL

    does this problem has any solution that i can get alert only on window close

    vineet

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vineet View Post
    hi OESXYL

    does this problem has any solution that i can get alert only on window close

    vineet
    use onunload instead, this will fire when the window is closed.

    regards

  • #7
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    hi

    did u mean write directly onunload without window.onunload. like this

    <script type="text/javascript">
    onunload = function()
    {
    alert("bye");
    }

    </script>

    vineet

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vineet View Post
    hi

    did u mean write directly onunload without window.onunload. like this

    <script type="text/javascript">
    window.onunload = function()
    {
    alert("bye");
    }

    </script>

    vineet
    Code:
    <body onunload="yourfunction();">
    other ways is like in your code or this way:
    Code:
    window.onunload = functionname;
    regards

  • #9
    Regular Coder
    Join Date
    Jun 2008
    Posts
    173
    Thanks
    2
    Thanked 9 Times in 9 Posts
    hi

    i tried both ways. both ways give alert on refresh. i m using IE7 and firefox 3.

    Code:
    <script type="text/javascript">
    function uload() 
    {  
    	alert("bye");
    	}
    window.onunload = uload;
    </script>
    vineet

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vineet View Post
    hi

    i tried both ways. both ways give alert on refresh. i m using IE7 and firefox 3.

    Code:
    <script type="text/javascript">
    function uload() 
    {  
    	alert("bye");
    	}
    window.onunload = uload;
    </script>
    vineet
    when you use refresh you load a new page and unload the current one => your function is called.

    regards

  • #11
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    When you refresh the page, click a link, close the browser, etc, they all fire an unload event because the page is exiting. There is no real way to determine if it is a refresh or a close.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •