How Can I Auto Hide Alert Box After It Showing It?
All I want to do is, how can I auto hide alert box within specific seconds after showing it? All I know is, setTimeout(function() { alert('close'); }, 5000); // This will
Solution 1:
tldr; jsFiddle Demo
This functionality is not possible with an alert. However, you could use a div
functiontempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
},duration);
document.body.appendChild(el);
}
Use this like this:
tempAlert("close",5000);
Solution 2:
You can't close an alert box with Javascript.
You could, however, use a window instead:
var w = window.open('','','width=100,height=100')
w.document.write('Message')
w.focus()
setTimeout(function() {w.close();}, 5000)
Solution 3:
impossible with javascript. Just as another alternative to suggestions from other answers: consider using jGrowl: http://archive.plugins.jquery.com/project/jGrowl
Solution 4:
You can also try Notification API. Here's an example:
functionmessage(msg){
if (window.webkitNotifications) {
if (window.webkitNotifications.checkPermission() == 0) {
notification = window.webkitNotifications.createNotification(
'picture.png', 'Title', msg);
notification.onshow = function() { // when message shows upsetTimeout(function() {
notification.close();
}, 1000); // close message after one second...
};
notification.show();
} else {
window.webkitNotifications.requestPermission(); // ask for permissions
}
}
else {
alert(msg);// fallback for people who does not have notification API; show alert box instead
}
}
To use this, simply write:
message("hello");
Instead of:
alert("hello");
Note: Keep in mind that it's only currently supported in Chrome, Safari, Firefox and some mobile web browsers (jan. 2014)
Find supported browsers here.
Post a Comment for "How Can I Auto Hide Alert Box After It Showing It?"