'Confirm' popup default value when blocked by browser - javascript

Is it possible to provide a default value for a basic javascript 'confirm' popup if the user has chosen to block popups in their browser (such as Edge)?
I'm looking to add a very simple confirm dialog on a web application to confirm an element is to be deleted.
Right now, I just have this line before the delete action :
if (!confirm("Delete this element?")) { return; }
// Code to delete the element here
Ideally, if the user chooses to block popups on their browser, the confirm popup should simply delete without further requests.

Related

window beforeunload is not working as expected in jquery

Basically what i am trying to do was, whenever a user tries to close the current tab(when he was on my site), i want to display a pop up with three choices about why he was leaving and want to store that choice some where
So i have written the following in main.js which will be loaded through entire site pages
$(document).ready(function() {
// Before closing the current tab, ask user for a reason
$(window).on('beforeunload', function(event){
$('#load_choices_up').click();
event.stopPropagation();
event.preventDefault();
debugger;
});
});
So i have three issues with the above jquery code
*.This code was executing even when i click another link on the same page(I mean if i navigate to another page from current page), but i only want this code to run when the current tab/page was closed(about to close) completely, but not when navigating to another page on my site
*. After this line $('#load_choices_up').click() was executed, a choices pop up was opening as expected, but immediately the default processing of browser(that is closing functionality) was not being stopped with two lines event.stopPropagation(); and event.preventDefault();, i mean these two methods of stopping the behaviour is not working and the browser is closed, but i want to do some processing based on user choices input and then based on that i will close tab.
*. When i used return "Why do you want to leave the page", instead of choices pop up, the browser was displaying different message based on browser type like "You have unsaved changes" in chrome, and some different message in firefox, instead of displaying my custom message
So finally, why event.stopPropagation(); and event.preventDefault(); are not working ? and why i can't able to display my custom message ?
You can't prevent someone from closing the browser. This is for obvious security reasons. Imagine a spam-website preventing you from closing the website while pumping you full of god knows what.
You can at most pull one function like an alert() or a prompt. After a user closes them, the tab will close either way.
beforeUnload is also extremely short-timed. You won't be able to run massive scripts with it, as the user would probably close the tap before any script would run properly. (I tried it with an ajax call, didn't work)
So, even if you're able to get the options you want in there, the moment a user chooses one of the options, you're not going to be able to save it anywhere. Your script will never make it that far.
You can customize the "are you sure?" message like so:
$(document).ready(function() {
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
});
but again, you can only change the text. It's a browser's native functionality, and you cannot change it.

Browser Tab Close Event for javascript or jquery

I have added a div which contains some survey questions which is initially hidden. Once the user closes the window or navigate away from the page, I need to popup the survey div to get the confirmation. I know there is a browser default confirm box method by using onbeforeunload event. But I need to popup the div rather than the browser default confirm box. Is there any way to do this? (or is there any way to interrupt the close event without even clicks on the "Stay on page" button found in the browser default confirm box?)
UPDATE : it seems we cannot do the action you are asking. There are some solutions to cover this in IE but overall its not possible. Ignore all the previous code.

How can I make a tool-tip to automatically appear for my chrome extension which uses pageaction?

I want to have a message displayed automatically, whenever my pageaction icon is displayed.
How can I make that to be displayed, I want to alert the user that " You can click this button to perform so on....".
If you want a popup appear programmatically, this cannot be done, it can only be initiated by a user click.
It would be nice if you could use infobars API, but it is still experimental.
Therefore, your only choice seems to be to inject a script into the page that would create some sort of overlay on the page roughly pointing to the page action area.
Note that you will need permission to access that page, as activeTab permission is not enabled until user clicks on your page action.

Javascript - Closing confirmation message if no response from user

I have got asp.net 2.0 website, in which I display session timeout through confirm message box, asking user to select ok or cancel.
Problem: if user doesn't provide any input (assuming his desktop is locked and he is out for lunch) - then how can I close this confirmation message and display another pop-up or re-driect to login page
Regards
If you are using the native browser confirmation box you cannot dismiss that, the user would need to do so. If you create your own, then you can use setTimeout to dismiss after a certain amount of time
As haslama suggested that you will have to create a custom alert as you can't dismiss the browser confirmation box.
As an alternative you can show a div based message to user instead of an alert, and in that div user can select OK or cancel and you can implement timeout for that div.

Javascript to close browser window simultaneously with dialog box

I'm working on an asp.net web form. This form has a button to open a new window (report window) and show a report. But first the user has to select a client from the list in the form before he/she can see the report. For validation matter, I use code to check whether the user has selected one of the client or not, the validation is done in the new window (report window).
The validation is set to show a javascript dialog box if the user hasn't selected one of the client, it's working fine, the dialog box show the message with a single OK button. Is it possible to close the window concurrent with the dialog box when the user click the OK button ?
Not with an alert window (you cannot get the event when the user clicks ok), you need either
a confirm instead of an alert so you can react on the event
or a "self-made" dialog like jquery dialog http://jqueryui.com/demos/dialog/ there you can react on the click event
And then just close the window with window.close()

Categories

Resources