Use of Alert Prompt and Confirm

using window.prompt is even worse - I always used to check the box to turn off JavaScript for the page whenever a web page displayed any of those antiquated debugging dialogs. Now I just have prompt, confirm and alert turned off completely in my browser so that I don’t see any debugging calls that people forgot to remove.

Anyone writing these JavaScript statements is presumably coding for Netscape 4 and is asking the question no later than 2005 when such code became obsolete. I am not sure how you find a time warp going back at least 10 years in order to even get questions about this antique code any more - JavaScript today is so different from what it was then that it has effectively been at least two if not three different languages since the version of JavaScript where that code was used.

Unless you need the ‘History of JavaScript’ course as part of a non-programming qualification the best thing to do is to ask to switch from the history class to a programming class where you can learn a current programming language rather than one that has bean dead for over 10 years.

No offense to the OP, but his/her English is obviously not the primary language. Depending upon what country the OP resides in, I would not be one bit surprised that the ancient JS methods are being included in lesson plans.

:slight_smile:

I am not surprised at all - there are plenty of places teaching historical JavaScript still all around the world.

It is so common that a couple of years ago I wrote a series of articles - http://www.felgall.com/net3p.htm - that compare the way that those teaching these history classes comparing the answers they’d expect with the way you would answer the question using JavaScript.

1 Like

You may be making a bad assumption.

document.write has a real, practical problem, and that’s why we avoid it. But the others – prompt, confirm, and alert – are still perfectly viable, and they still get used in real-world applications today. The alert box even got a makeover in Firefox some time ago to look more modern.

As I said - I got fed up with clicking the checkbox to turn off JavaScript for the web pages as these calls ask me to so I disabled them completely in my browser.

They were replaced in all browsers more modern than Netscape 4 by something called the Document Object Model - for displayin g things to your visitors - and the dialogs were repurposed for debugging. That more recent use has now been replaced by console.log() meaning that there is no reason to keep the dialogs enabled in the browser - after all maybe I don’t want to turn off JavaScript just because the page owner decides to ask me to.

The application APIs and the DOM are two separate things. Neither replaced the other.

That’s correct. Before we had console, we used alert for debugging. But that doesn’t mean alert can only be used for debugging. We can still use it for its original purpose – to display an alert message to the user.

The page owner isn’t asking you to. The browser is giving you the option to. And even then, the browser gives you that option only if too many alerts are displayed in rapid succession.

Those dialogs images I included are the way that ALL such dialogs look in my browser (or would if I hadn’t disabled them). So based on what you just said displaying ONE alert is displaying too many in quick succession. Before I turned off alerts completely in my browser I always used to check the box to turn off JavaScript when the first alert appeared on the assumption that since the dialog is intended purely for debugging and they obviously haven;'t finished debugging their script yet that turning off JavaScript for that page is the best option. Now with those dialogs turned off JavaScript stays on for the page because I no longer see those dialogs.

The thing to keep in mind is that every browser displays these dialogs differently but that you can create one that looks the same in all browsers and which can’;t be turned off by creating it as a part of the web page using the DOM.

When you use alert,confirm and prompt you have no control over how they look or whether your visitor will see them at all. If you create your own then you can display the same thing regardless of what browser your visitor is using and they can’t turn it off without turning off JavaScript. The most recent browser that didn’t allow you to create your own dialogs so as to present the same appearance in all browsers was Netscape 4.

While only about 1 in 200 people are using the browser that displays the option to turn off JavaScript for the web page in EVERY SINGLE alert, confirm and prompt debugging dialog it displays, it will also display dialogs differently in other browsers as it is dependent on the browser being used (assuming that the browser owner hasn’t turned them off - the confirm dialogs asking if you really meant to try to leave their web page are particularly annoying so I’d imagine that anyone who knows how to turn them off will have done so).

Then you must be using something either very old or very weird. Before I posted, I checked how those dialogs behave in the latest Chrome, Firefox, IE, and Opera.

Plus, it still doesn’t change the fact that: “The page owner isn’t asking you to. The browser is giving you the option to.” To think that the page owner was asking you to disable JavaScript is a bit of a bizarre conclusion for you to come to.

Again, you’re making a bad assumption.

That’s an option, yes, and a more powerful option, I grant you. But alert still works and is also still an option. And it’s an option that has its own advantages. It’s simple with no frills. I’ve seen Google use alert before, because some of their apps are a no-frills style. And simple is especially advantageous for newbies, like the OP of this topic. He doesn’t need to know (yet) how to make widgets from a combination of HTML, CSS and JS. He just needs to collect some simple input and show some simple output so that he can focus on the real logic he’s trying to learn at the moment.

You keep wanting to treat alert/prompt/confirm as if they are cardinal sins. They aren’t.

2 Likes

You have forgotten that there are TWO different latest versions of Opera. Half of Opera users are still using Opera 12 which still reports itself as the latest version and that is the one that displays the turn off JavaScript option in every single dialog. That version of Opera will continue to be a latest version until all of the features that browser has and that the other Opera doesn’t are ported across - those still using it are using it because of the features that the other browsers haven’t copied yet.

Anyway I still find these dialogs to be annoying and so turn them off in all of the browsers that I have on my computer.

If people do use those dialogs in their scripts then they can’t rely on people seeing them as they are easily turned off. If they want to guarantee that people with JavaScript enabled will see their dialogs then they need to create their own.

There’s actually no guarantee of that at all. Any user can screw with the DOM at any time, just like you can screw with your JS environment at any time. And in fact, we mess with the DOM on a regular basis with things like ad blockers. DOM dialogs are no more or less guaranteed than alert dialogs.

Please publish a simple script that will turn ALL in page DOM generated alert dialogs off the way that a single line of JavaScript can turn off all window.alert() calls then.

I’ve never really understood this either, felgall.

Sure, console.log is the better alternative 99.5% of the time, but there are lots of occasions when alert() or confirm() is fine. For example, I’m just working on a simple CRUD app. If the user clicks to delete a record, I use a confirm() ensure that this is really what they want to do.

Also, alert() etc block JS execution until the user does something, which is quite handy sometimes.

But with some browsers confirm has three choices - OK, cancel and turn off JavaScript for this page - with turning off JavaScript being the most noticeable of the three and therefore presumably the most likely option of the three for people to choose.

If you only want to give two choices then you can’t use confirm.

It’s about three minutes before midnight here, so I’ll say Happy New Year!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.