...

View Full Version : how to call function after confirm ok



vineet
11-22-2008, 02:05 PM
hi all

i have two function each doing separate work.


<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

oesxyl
11-22-2008, 03:49 PM
hi all

i have two function each doing separate work.


<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


<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

vineet
11-22-2008, 04:07 PM
HI OESXYL

thanks for the reply. that is working fine.

i would like to ask you one more question that i m using


<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

oesxyl
11-22-2008, 04:10 PM
HI OESXYL

thanks for the reply. that is working fine.

i would like to ask you one more question that i m using


<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

vineet
11-22-2008, 04:16 PM
hi OESXYL

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

vineet

oesxyl
11-22-2008, 04:21 PM
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

vineet
11-22-2008, 04:24 PM
hi

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

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

</script>

vineet

oesxyl
11-22-2008, 04:29 PM
hi

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

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

</script>

vineet


<body onunload="yourfunction();">

other ways is like in your code or this way:


window.onunload = functionname;


regards

vineet
11-22-2008, 04:43 PM
hi

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



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



vineet

oesxyl
11-22-2008, 04:46 PM
hi

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



<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

A1ien51
11-22-2008, 08:50 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum