close window in button click using javascript - 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();

Related

Set Focus to a popup window from C# (or JS)

I'm trying to get a popup window to focus i tried the window.open().focus();
but it keeps changing in the background so I'm trying in my code to close a window and re-opening it again.
string url = "Report.aspx";
string s = "window.open('" + url + "', 'popup_window456789123', 'width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes')";
string t = "window.close()";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", t, true);
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
Can anyone tell me why this doesn't work?
I tried it in Jquery but that didn't work either:
<script type="text/javascript">
$(document).ready(function () {
alert("test");
window.focus();
});
</script>
Flow:
Main page: click button1 ► opens popup1
popup is in focus and shows data
mainpage(gets focus): click button2 ► opens same popup1
popup changes data (but dos not get focus) This is the issue
Ok I've done a fiddle which shows what you should have. View Fiddle
If I click off one window and click on another then it comes back (reloads) and gets focus.
If you are expecting something like a text box getting focus or something in the loaded window then you need to have something within the page that is getting loaded to give that text box focus.
<script> function openwin(url) {
window.open(url, 'popup_window456789123','width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes ')
}
</script>
<button onclick="openwin('https://www.webpagetest.org/')">
Button1
</button>
<button onclick="openwin('https://www.webpagetest.org/')">
Button2
</button>

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.

how to prevent close dailogbox pop up when close dialog and refresh

Hi it is very simple question, but I didn't find the answer fit in my situation.
In my jQuery dialog I have buttons which is update and close. User click 'update button' then on code behind I need to update in database then close.
when user click the close button then I close the dialog. the dialog is load aspx page. I have two problems.
The problem is if user click close button which run javascript window.close(). It will pop up the windows "Do you want to close window...."
How can I refresh the parent page and close the dialog by clicking the 'update' button.
there is the code to load the dialog:
function openDialog(url, name, width, height) {
$("#dialog-box").load(url).dialog({
autoOpen: false,
resizable: true,
height: 425,
width: 600,
modal: true
});
$('#dialog-box').dialog('open');
return false;
}
I tried to use $("#dialog-box").dialog("close"); in my function which is called in code behind. but it show the error.
there is my function
function RefreshParentAndClose() {
$("#dialog-box").dialog("close");
}
there is the code I load the dialog on parent page
<div id="dialog-box" title=" "></div>
<td width="33%" align="right"><asp:button id="btnSelect" runat="server" causesvalidation="False" text="Select Locations"
OnClientClick="javascript:return openDialog('popLocation.aspx','select',600,500);" /></td>
Use below code for close
$("#dialog-box").dialog( "close" );
and to refrsh on close use
$("#dialog-box").dialog({
close: function( event, ui ) {
window.location.reload();
}
});
1) You could close the dialog by invoking its close() method:
function OnCloseClick() {
$("#dialog-box").dialog("close");
}
2) You just need to refresh the main page. On the subsequent load the dialog should not be opened (in case you do not load it automatically). More information about the method's parameter used below could be read from this answer on StackOverflow.
function OnUpdateClick() {
window.opener.location.reload(false);
}

Javascript alert() in a pop up window

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.

Categories