为什么我不应该在JavaScript中使用Alert? [英] Why shouldn't I use Alert in JavaScript?

查看:107
本文介绍了为什么我不应该在JavaScript中使用Alert?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在某些情况下通知用户并且已经看到不应该使用confirm()和alert()。相反,很多问题都提到在jQuery UI中使用模态事物,特别是如果你需要设计样式。这一切都很好,但是,在一天结束时,编码员说你应该远离它的原因是什么?

I need to notify the user under certain circumstances and have seen that confirm() and alert() shouldn't be used. Instead, a lot of questions mention using the modal thing in jQuery UI, especially if you need to style things. That's all fine and dandy, but, at the end of the day, what's the reason coders are saying you should stay away from it?

这个是一个很好的例子,说明人们的感受,但几乎任何其他问题都在处理样式警报框具有相同的注释。它只是一个样式问题还是有更多根本原因可以回避这种内置功能?

This is a pretty good example of how people feel, but pretty much any other question dealing with styling an alert box has the same comments. Is it only a styling issue or are there more fundamental reasons to shy away from this built-in functionality?

更重要的是,没有一个答案谈论你应该做什么改用。 jQuery UI和类似的预建解决方案是唯一的答案吗?为什么?性能或安全性或缺乏经验的编码员是否需要易于使用的东西或......?

More importantly, none of the answers talk about what you ought to use instead. Are jQuery UI and similarly pre-built solutions simply the only answer? Why? Is it performance or security or inexperienced coders who need something easy to use or...?

编辑:

好的,让我澄清一下。我问的原因是因为警报功能是为了防止用户意外删除他们一直在做的工作,因为有三种方法可以在这个页面上启动新计划,并且每个方法都会清除现有的内部html。显示计划的div。

Okay, so let me clarify a bit. The reason I'm asking is because the alert functionality is to prevent a user from accidentally wiping out the work they've been doing since there are three ways to start a new plan on this page and each wipes out the existing inner html for the div where the plan is displayed.

我听到的大部分内容都是对用户和用户界面有害,但是,如果引起愤怒,引起注意力是我想要的,我该怎么用代替?有人提到警报/确认会暂停JavaScript执行,并可能阻止您使用其他选项卡。这更接近我期待的功能问题。我确实理解这对UI有害,但我在这里寻找更多技术原因。

Most of what I'm hearing is that it's bad for users and UI, but, if the anger-inducing, attention grabbing is what I want, what should I use instead? Someone mentioned that alerts / confirms halt JavaScript execution and may prevent you from using other tabs. That's closer to the functionality problems I'm expecting. I do understand that it's bad for UI, but I'm looking for more technical reasons here.

推荐答案

没有技术原因您不应使用内置浏览器方法进行警报和确认功能。这一切都是自以为是。

There are no "technical reasons" that you should not be using the built-in browser methods for alert and confirm functionality. It's all opinionated.

让我们把它分成两部分。

lets split this into two parts.

警告不应该用于调试 除非 您打算为了某个目的而停止执行代码。否则,您应该使用 console.log ,因为如果您的代码涉及异步逻辑,alert实际上可以更改代码的结果。 但IE中不支持console.log!是的,如果控制台是打开的。在生产代码中,您的调试代码不应该在那里。为了防止被遗忘的console.logs,只需将其填充即可。对于除调试之外的用途,例如通知用户发生了某些事情,通常最好使用其他方式通知用户发生了更改,例如突出显示元素或带有信息文本的横幅,但是,没有什么错误在这种情况下使用警报,只要暂停执行不会影响您的代码。

Alerts should never be used for debugging unless you intend for it to stop the execution of the code for a purpose. Otherwise, you should be using console.log because alert can actually change the result of your code if your code involves asynchronous logic. "But console.log isnt supported in IE!" Yes it is, if the console is open. In production code your debugging code should not be there anyway. To protect against forgotten console.logs, simply polyfil it. For uses other than debugging, such as informing the user that something happened, it's generally better to use some other way of informing the user that a change happened such as highlighting an element or a banner with informational text, however, there's nothing wrong with using an alert in that case as long as the pause in execution won't impact your code.

确认是询问用户的标准方式你确定吗?在执行诸如删除信息之类的破坏性操作之前。它很容易理解并被许多网站使用,所以我建议在你自己的代码中使用它。替代方案通常会导致额外的代码为您的应用程序添加不必要的复杂性。但请记住,确认也会阻止执行,所以你应该确保它不会影响你可能运行的任何异步逻辑。

Confirm is the standard way of asking a user "Are you sure?" before performing a damaging action such as deleting information. It's easy to understand and is used by many sites, so i'd suggest using it within your own code as well. The alternative will generally result in a extra code adding unneeded complexity to your application. Keep in mind, however, confirm also blocks execution, so you should make sure that it won't affect any asynchronous logic that you may have running.

如果你的异步代码如果写得正确,它通常不会受到暂停执行的影响。

If your asynchronous code is written correctly, it generally won't be impacted by a pause in execution.

这篇关于为什么我不应该在JavaScript中使用Alert?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆