I want to be able to close an alert box automatically using Javascript after a certain amount of time or on a specific event (i.e. onkeypress). From my research, it doesn't look like that's possible with the built-in alert() function. Is there a way to override it and have control over the dialog box that it opens?
Also, I don't want an override that shows a hidden div as the alert. I need an actual dialog box.
As mentioned previously you really can't do this. You can do a modal dialog inside the window using a UI framework, or you can have a popup window, with a script that auto-closes after a timeout... each has a negative aspect. The modal window inside the browser won't create any notification if the window is minimized, and a programmatic (timer based) popup is likely to be blocked by modern browsers, and popup blockers.
Appears you can somewhat accomplish something similar with the Notification API. You can't control how long it stays visible (probably an OS preference of some kind--unless you specify requireInteraction true, then it stays up forever or until dismissed or until you close it), and it requires the user to click "allow notifications" (unfortunately) first, but here it is:
If you want it to close after 1s (all OS's leave it open 1s at least):
var notification = new Notification("Hi there!", {body: "some text"});
setTimeout(function() {notification.close()}, 1000);
If you wanted to show it longer than the "default" you could bind to the onclose callback and show another repeat notification I suppose, to replace it.
Ref: inspired by this answer, though that answer doesn't work in modern Chrome anymore, but the Notification API does.
no control over the dialog box, if you had control over the dialog box you could write obtrusive javascript code. (Its is not a good idea to use alert for anything except debugging)
I want to be able to close an alert
box automatically using javascript
after a certain amount of time or on a
specific event (i.e. onkeypress)
A sidenote: if you have an Alert("data"), you won't be able to keep code running in background (AFAIK)... . the dialog box is a modal window, so you can't lose focus too. So you won't have any keypress or timer running...
Try boot box plugin.
var alert = bootbox.alert('Massage')
alert.show();
setTimeout(function(){alert.modal('hide'); }, 4000);
I guess you could open a popup window and call that a dialog box. I'm unsure of the details, but I'm pretty sure you can close a window programmatically that you opened from javascript. Would this suffice?
The only real alternative here is to use some sort of custom widget with a modal option. Have a look at jQuery UI for an example of a dialog with these features. Similar things exist in just about every JS framework you can mention.
If you do it programmatically in JS it will be like reinventing the wheel. I recommend using a jQuery plugin called jGrowl
You actually can do this you can basically listen for this pop up to happen and then confirm true before it ever "pops up",
if(window.alert){
return true
}
You can use label and set its fade in and out time for e.g
Hide it initially and show on click.
$('#div_Message').fadeIn(500).delay(1000).fadeOut(1500);
window.setTimeout('alert("Message goes here");window.close();', 5000);
Related
I have an alert that comes up in chrome, it slides into the browser on the top right hand side of the window. Every time this specific alert comes up I have to move my mouse over and click the alert box which will then open up a link in a new window. I have to do this multiple times a day. I'd be great if I could write a Javascript or Python program that will automatically "click" the box every time it comes up in the browser. Is there a way to do this? I have a little over a year of programming experience but I don't expect a step by step answer, just set me off in the right direction. I've looked up browser events hoping I could just write an event listener function but I couldn't find one specifically for browser alerts.
If it is the native modal of the browser, then the short answer is no.
You can't do it with javascript as the confirm (alert) pop up is a blocking modal.
No javascript code can run while the confirm modal is open.
If this is a custom modal that built and controlled by javascript then there is a good chance of doing just about anything you want with it.
How can I avoid the checkbox 'Stop execute scripts on this page' in the Javasript message box?
Regards,
David
Quite simply, you can't. It's placed there for a reason - to stop sites that try to prevent you from leaving by popping up a new dialog box everytime you try to exit. In most browsers, like #Bazzz said, they don't show this until the second or third alert in a row.
You can't do that. It's a built-in feature in some browsers to prevent alert/confirm dialogs loop which can block whole browser. I can't image situation when many dialog downloads can be a good user experience.
Anyway, you don't have to rely on browser - just use custom dialogs in HTML/CSS which you can control as you wish.
when i try to
window.open()
in IE 9 , it opens it with favorites sidebar (if it was present in parent window) this is behaviour unique to IE , and it breaks dialog windows as I envisioned them. Any hope to fix that?
Since you specified that you're using this for a dialog, I feel I should discourage this. Using window.open() is not ideal for creating dialog boxes.
Some browsers will ignore your 'new window' request, and open it as a new tab. This can be configured by the browser user, so is out of your control.
If the user has toolbars and side panels open, there's a strong likelyhood of them showing up in the new window, which will mangle your layout. Again, you'll need to test this in every browser, and even then you can't be sure without knowing all the config options that might affect it.
Opening a new window does not give you a modal dialog box. You can't prevent the user from clicking back to the parent window and ignoring the dialog box.
Therefore, if you want to make a dialog box, you would be much better off using a javascript library that opens a box inside the current page. It's much more flexible, and gives you much more control over the end result than window.open().
If you're using JQuery, you might want to start by looking here: http://choosedaily.com/1178/15-jquery-popup-modal-dialog-plugins-tutorials/, but there are stacks of others available (it's a very easy thing to write, especially in JQuery, so there's plenty of plugins out there you can try till you find one which is perfect for you)
Try changing it to window.location.href= 'url + target="_blank"'
As stated in the Title, i'm trying to figure out if it's possible in Javascript to:
Open a popup through my parent site with window.open
Have that popup display in front of the parent
Not lose keyboard focus from the parent window.
So something like the functionality of the popup notification of Windows Messenger for example
Any ideas?
Regards,
user523842 :P
Don't use window.open use instead one of many many alternatives aka overlay window/div.
You can use pure JavaScript for this:
http://library.creativecow.net/articles/chaffin_abraham/full-page-overlay.php
Or one of many jQuery solutions as well, just Google for it.
Not afaik. You can't have a focus window underneath a blurred one, but you shouldn't rely on window manipulation anyway because there's a high probability the thing will open in a tab or some other unpredictable client controlled manner. If you want JS dialogs and tight control, use lightbox techniques.
Yes, I realize this is horrible UI and bad accessibility wise, but I am forced to seek out the options due to contracted work ( to which I didn't initially agree upon and am stuck with ).
I know that you can assign an event handler to onbeforeunload like:
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
That would bring about a dialog generated by the OS/browser which cannot be customized. It would ask you to Cancel your request or go on.
So far it seems the only way I can display any custom-ness is by inserting a window.open in that event handler:
window.onbeforeunload = function() {
var x = window.open('modal.html');
}
This would most likely get blocked by any modern browser as a "popup". I have to display an entirely new page with my dialog, but this seems to be the only way to meet the demand.
Is this pretty much the only option I have? Other than forcefully telling the client it isn't recommended to do this?
The only other option would be relying on the user to hit "Cancel request" and then insert the dialog.
Questions I have already looked at:
jQuery UI Dialog OnBeforeUnload
How can I override the OnBeforeUnload dialog and replace it with my own?
As per the questions you already looked at, you cannot customize that dialog beyond passing in a string message to be displayed by the browser.
window.onbeforeunload - MDC
I just went the window.open route onbeforeunload.
you could use a hidden Flash .swf that you reveal at onbeforeunload time instead of window.open