I have a content management page on my CMS on which I have a popup that warns the user on page close. It is a very simple warning implemented like so:
window.onbeforeunload = function (e) {
return 'Are you sure you want to close?';
}
It works, but the problem is that the popup is shown even when the user clicks the same button. I am very new to document events so I have no idea how to do this, but I want the popup to only show when the page is being closed and not when it is being posted back.
Also I have not used JQuery in my website so please don't suggest it; I don't want to use a whole framework just for a popup.
.
Thanks.
I was able to get it to work by using a global variable, like so:
var check = true;
Which is checked for when showing the popup:
window.onbeforeunload = function (e) {
if (check) {
return 'Are you sure you want to close?';
}
}
Then you simply set check to false when you click a button that does a postback. Hope this helps someone.
Related
Does anybody know how to open an URL in a new tab from C# code?
I have tried with
Response.Write("<script type='text/javascript'>window.location.href('../Documents/doc.pdf','_blank'); </script>");
Response.Write("<script type='text/javascript'>window.open('../Documents/doc.pdf','_blank'); </script>");
Response.Write("$('#pageContent_Send').click();");
with
$("#pageContent_Send").click(function () {
window.open("../Documents/doc.pdf");
return false;
});
and it did not work, using "window.open" I get an "Pop-up Blocker" browser warning.
There are a few options for opening a new tab that won't get blocked.
You can have it open the URL with an anchor click, like so click me
You can submit a form to a blank target, giving a similar result, but from a form. This is useful if you need to post, but can also be useful for get. Like so <form method="get" action="<your destination>" target="_blank"><button type="submit">Click Me</button></form>
You can use JS with window.open but it has to be tied to an active click event. I see in your original post that this got blocked, but if you're triggering it on a click event directly (i.e. not using a setTimeout or an async call or something else that delays the window opening) then this should work in all browsers. Since you're trying to "fake" it by forcing a click, the browser is going to block this every time unless you explicitly allow it.
<a href='www.xyz.com' target='_blank'>Click me to open me in new tab</a>
I need my web page to open a window and enable a disabled button when a link or a button is clicked.
From what I've read on other posts on here, if I try and open a new window in Page_load most browsers will assume it's a pop-up and block it so I've been trying to do it client side with JS.
Currently, I'm trying it with a link declared like so:
Please click here to open the document.
This calls the following JS:
function OpenDoc()
{
<%= btnSubmit.ClientID %>.Visible = true;
Window.Open('GetDocument.aspx')
}
Unfortunately, instead of rending the JS as "btnSubmit.Visible = true" it comes out as "MainContent_btnSubmit.Visible = true" which doesn't work.
Assuming this is the best way of doing what I want, where am I going wrong?
You can't change visibility property through javascript but you can use the following code instead of it :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.disabled = true;
In this case the button will be disabled and if you need to hide button not disableing it then use the following code :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.style.display= "none";
Hope this is helpfull based on my understanding to your problem
I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?
I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?
This is one of the multiple ways to achieve the same thing
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
got it from here open js
Only the unload() event will work on JS. You can't manage it on the server.
Check out the answer to this other question on SO, it is very similar to your question
How to show the "Are you sure you want to navigate away from this page?" when changes committed?
Simple solution
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Are you sure you want to leave this page?";
}
4guysFromRolla.com - Prompting a user to Save when Leaving a Page
You cannot use the onbeforeunload window method as it gets triggered by multiple ways like back and forth browser navigation links, refreshing the page, closing of the page, clicking on the links.
What i feel you have to bind the link tag for which you want display the navigation away message and then use the function for the status message display
window.addEvent('domready',function(){
$$('a').addEvent('click', function(e) {
//leaving(); function u wrote for displaying message
});
});
function leaving(e) {
if(!e)
e = window.event;
// return code for the displaying message
}
If you want to do this in a way that guarantees it will work on almost all browsers, use the JQuery library. The following describes the unload event.
http://www.w3schools.com/jquery/event_unload.asp
It's exactly for purposes like yours.
Just to elaborate a little, you would have to download the jquery js library and reference it in your project/page, but you'll probably want to do that eventually anyway.
If you want to control this from the server side, you can dynamically emit the jquery call in the OnPreRender.
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.
I was able to get this to work with Andrei G's answer. I would add on that to get it to work in Chrome, add this to the end of his goodbye function:
return "";
I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.
Is it at all possible to disable the "The webpage you are viewing is trying to close the window. Do you want to close it?"?
I understand that this is the product of years of virus, and malicious script activity, but in legit app code, (ASP.NET), is there any way to like "register" your app as an APP, or flags you can pass to an IE Popup so that it will not display this when it closes?
The code I'm using is done from within the C# code behind:
ClientScript.RegisterClientScriptBlock(GetType(), "save", Utils.MakeScriptBlock("window.close();"));
The Utils.MakeScriptBlock is just a function that does what you might expect. It 'injects' a <script...> tag with the code in it...
It's probably not possible to get around this, or else, all the script kiddies would just use that trick, but I thought I'd ask, as I can't be the ONLY one using simple IE "popups" as (pseudo)modal dialog boxes.
This code happens in my ButtonSave_Click() routine, after everything has passed validation, etc...
** EDIT **
Just for reference, here is the code that OPENS the popup, when the ADD button is clicked:
This is in Page_Init()...
ButtonAdd.Attributes.Add("onclick", "window.open('Add.aspx', 'ADD_WINDOW', 'scrollbars=no,width=550,height=550,location=no,menubar=no,resizable=yes,directories=no,status=no,toolbar=no'); return false;");
You can close the window without the popup if the window was opened by your script. Does that help?
Edit:
You're already opening the window with script. Change your client script to call self.close().
ClientScript.RegisterClientScriptBlock(GetType(), "save", Utils.MakeScriptBlock("self.close();"));
I believe not. As you state to help prevent malware.
In the end the browser does not know that you are not evil. See RFC 3514 for a similar idea.
I highly doubt it's possible on your end - sounds like something a user would have to specifically disable in their IE settings.
If the user opened the browser window, you can't close it; but if the window was opened via script, you can. Sounds like you just need an initial page that the user starts at with a "click here to begin" link, from which you open a new window for the main portion of the site; when they're all done, close the popup and they're left with their initial browser window with the "click here to begin" message.
Bizarre, but I've found that
<script type="text/javascript">
function closeWindow()
{
window.close();
return false;
}
</script>
and then calling
return closeWindow();
usually gets around this.