Javascript alert() in a pop up window - javascript

I have an anchor tag whose click event calls a JavaScript function, which opens a pop up window.
Now when the user clicks the close button of the pop up window I want to generate an alert(), which asks whether the user wants to close the pop up window or not
Using the below code the alert is generate in the parent window rather than the pop up
<script>
function openForm()
{
var new_window = window.open('file:///C:/Users/Tejora/Desktop/test.html','popup''width=825,height=585,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=0');
new_window.onbeforeunload = function(){
alert("Are you sure you want to close the window"); }
}
</script>
Click
Thanks in advance...

When a string is returned from the onbeforeunload handler, the browser will display a confirmation dialog with that message. Replace this line:
alert("Are you sure you want to close the window");
With this:
return "Are you sure you want to close the window?";
Here is the relevant documentation.

Related

Confirm close window popup does not appear on first time

I am trying following code to confirm user if they really want to close window.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
But it does not work when my page load and I try to close page [X] without clicking anywhere on page. Yes it does appear when I click somewhere on page then close [X] window. I don't know why do I need to click somewhere on page and then close window.
What I tried so far but nothing works.
window.focus() on document ready as it gets focus on window.
$('#someElementOnpageId').click() as it gets auto click on document
ready.
Can anyone please suggest what should I do?

How to close color box popup on browser back button

I want to close my color box popup on browser's back button click.
I used an iframe and inside the iframe there are many links. When user click on particular link then a color box popup will open.
Currently after opened the popup if user click on back button then popup is not closing.
So i want such a functionality that if user click on back button then popup will be close and also page should not go to back (Disable back button).
I have used the following code but it's not working for me.
<script>
$(document).ready(function() {
function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
</script>
Please give me any suggestion to close the color box popup on back button or disable the back button completely.
You should use window.location.hash, and window.onhashchange.
Basically, after popup open, you change hash to something (like window.location.hash = "popup_opend").
Then when user clicks back, that back click will remove the hash you just added.
You just need to handle window.onhashchange to check for that and close the popup.
Of course, hope you don't have any other code that manipulate the hash.

Supress/Bypass IE alert message on closing the browser

I have a web page which has a ajax modal popup being loaded inside it.When the modal popup is there we need to restrict the user from closing the browser.we have handled the onbeforeunload event but it showed a popup "are you sure you want ot leave this page".How to supress this popup and stop the page from closing.
<script>
window.onbeforeunload=function() {
window.alert("Hai");return false;
}
</script>
Unfortunately you can never prevent a browser window from being closed as that would lead to all kinds of abuse (imagine a porn site opening a window you cannot close).
The onbeforeunload eventhandler should return a string that could explain to the user what will happen if he closes the window, as the returned string is shown in the confirmation box. E.g:
window.onbeforeunload = function() {
return "If you close the window your unsaved changes will be lost!";
}
That string combined with the non-overridable confirmation dialog with OK and Cancel buttons will hopefully steer the user into making the right decision.

close window in button click using javascript

I'm creating message dialogue box using JavaScript alert statement. That dialogue box contains one button. In that button click i want to close the current window.
JavaScript
<script language="javascript" >
function Confirmation() {
var win = window.open('', '_self');
if (confirm("Are you sure you want to close the form?") == true)
win.close();
else
return false
}
</script>
HTML
<td class="style5">
<asp:ImageButton ID="close" runat ="server" ImageUrl ="image/button_blue_close.png" OnClientClick="return Confirmation()"
Height="16px" Width="21px" ImageAlign="Right"/>
</td>
my code only close the window when i click the image button. But i want to close the window in button click of the dialogue box
my code in vb.net
-----------------
ClientScript.RegisterStartupScript(Me.GetType(), "message", "alert('Please Raise the ticket for particular event');", True)
You should change your code like that, if you want to close your parent window
`<script language="javascript" >function Confirmation(){if (confirm("Are you sure you want to close the form?") == true) window.opener.close();else return false;}</script>`
If I understood you question properly, you want to close your parent windown from dialog box. Though the code you have written will close your dialog box and not your parent window. To close your parent window, you can use below code:
window.opener.close();

Javascript alert when user closes the tab or the window [duplicate]

I want when a user closes the tab or window or when he tries to move to another location different from my site to pops a confirm box, and if he confirm to execute an ajax script and then to close or change the window. I don't know how to do that. PS: I'm using jQuery.
$(window).unload(function() {
var answer=confirm("Are you sure you want to leave?");
if(answer){
//ajax call here
}
});
Just add your own alert/dialogue code to the function.
<script language="JavaScript">
function unload() {
alert('window closed');
}
window.onunload = unload;
</script>

Categories