最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Don't allow users to stop javascript alert() - Stack Overflow

programmeradmin5浏览0评论

I'm developing a MVC4 web aplication.

I use javascript and jquery for a lot of stuff. But for info like this:

I use the alert() function to display various messages to the user. The problem is, that the user can stop this alerts from appearing. And if the user does that, a lot of important messages and info won't be showing to him.

How can i make that the user cannot stop the alerts from appearing?

Sorry for my bad english. Thanks

I'm developing a MVC4 web aplication.

I use javascript and jquery for a lot of stuff. But for info like this:

I use the alert() function to display various messages to the user. The problem is, that the user can stop this alerts from appearing. And if the user does that, a lot of important messages and info won't be showing to him.

How can i make that the user cannot stop the alerts from appearing?

Sorry for my bad english. Thanks

Share Improve this question asked Jul 30, 2013 at 20:03 GalloPintoGalloPinto 6772 gold badges11 silver badges25 bronze badges 4
  • 23 You can't--thank god. – Brad Christie Commented Jul 30, 2013 at 20:04
  • You have to create your own alert-like functionality (a faux modal window). And I alert (hehe) you: it will have to be asynchronous, so the way you currently call the alerts may have to change. – bfavaretto Commented Jul 30, 2013 at 20:06
  • Answer: Don't use alerts. – AlbertEngelB Commented Jul 30, 2013 at 20:08
  • I think can probably do this, by using setTimeout or setInterval. I still think it's a terrible idea but this fiddle demonstrates what I mean: jsfiddle.net/jEpTF the checkbox to disable the alert messages will never appear. – basilikum Commented Jul 30, 2013 at 20:11
Add a comment  | 

7 Answers 7

Reset to default 8

No.

You can't stop users from stopping alerts from appearing.

Alerts are very annoying:

  • They pop no matter what and steal focus
  • They stop whatever you're doing on the page

Alerts belong to the browser and not the website. Similarly, avoid prompts and confirms (confirms have a legit use case).

Moreover, just to prove the point - it's impossible from userland JavaScript. To prove this let's check the source code in charge:

Inside void WebContentsImpl::RunJavaScriptMessage

if (suppress_this_message) {
    // If we are suppressing messages, just reply as if the user immediately
    // pressed "Cancel".
    OnDialogClosed(rvh, reply_msg, false, string16());
}

In turn suppress_this_message is being set as 'true' when you click the checkbox.

What can be done?

Consider using modals instead. They are better design wise.

Modals are:

  • Inside the page
  • Don't block execution
  • Let you control how they look at how completely and across browsers, how they are closed, and how they interact with the page.

But how?

  • Create an element with fixed position (css position:fixed , or with JavaScript with position:absolute)
  • Set its display to hidden, and style it like the 'alert' window, only with style that fits with the site.
  • Show it when the button is clicked, you might want to darken parts of the screen too.
  • When the user clicks close set its display css property to hidden.

Lots of popular frameworks like jQueryUI come with modals.

You can't--thank god.

This was implemented by a lot of different browsers because of webpages that would flood the user with an alert (or several hundred) forcing them to dismiss every one of them until you could close the window (or you ctrl+alt+del and close the browser)--see which you decide.

As an alternative look to using a growl/toast message, or some form of dialog (like jQuery-UI Dialog). Also, using alert(), by today's standards, is an antiquated way of prompting the user (given the plethora of other options available).

You can't disable this behaviour. If it is vital that users see all your notices, don't use alert(), use a modal window instead.

You can not prevent the user from disabling that, instead consider using something like http://jqueryui.com/dialog/#modal-message

jQueryUI is included in the .net MVC4 template :D

You can't stop that checkbox from being there. It is a feature implemented in the browser for the user to choose to block alerts when a site is overusing them. You also can't stop the user from checking that box, but if your messages are important enough to them they surely won't.

You should consider using modal windows, since you can style them as you like and they don't block scripts / lock the browser on that tab.

You should not use alert to show your users important messages. Instead show the messages inside the web page itself. There are numerous plugins, libraries, etc to help you with this. One example is Bootstrap http://getbootstrap.com/components/#alerts-default

The user can stop those alerts from showing up.

You can, however, detect if those alerts are showing:

const start = Date.now();
const question = "Are you sure?";
const confirmed = window.confirm(question);
const duration = Date.now() - start;

if (duration < 10) {
  // Confirm dialog is disabled
} else {
  // Handle confirmed
}
发布评论

评论列表(0)

  1. 暂无评论