Settings for javascript Alert

Asked

Viewed 2,260 times

8

Are there other ways to change the Alert display? for example when using \n he breaks the line, I wonder if there are other commands of those that I can modify the text display, for example leave a sentence in bold and etc

alert('Texto\n Linha de baixo');

I know I could use a custom Alert using divs and such, but I was really curious about the alert()

Most interesting example

alert('Error:\n\n\tPrint plugin is loaded but NOT active!');

They realize that the text Print plugin is loaded but NOT active! gets a different format

  • Wouldn’t it be better to create a dialog customized?

  • 1

    @Felipeavelar would be better, but I’m curious about that ;)

  • @Silvioandorinha, I was also curious, but researching seems that it does not apply any type of styles to alert native, as informed in this question.

  • Good question! I think that can not do much, no, and I thank heaven for it (the functions alert, confirm and prompt should be extinguished, they are a nightmare for security and usability...).

  • @Fernando and @mgibsonbr Look how interesting the example I found, click to execute the text of the code alert gets a different color

  • I believe there is no command for the alert, the \n is converted to a character line break when placing in a string, ie the alert just displays the string, without formatting anything.

  • You can use other special characters that are converted when using in a string, such as the tab character \t or even icons, such as \u2605. alert("teste \u2605\n\tlegal");

Show 2 more comments

2 answers

4


The alert is a "pure" text window, meaning markup languages will not work, let alone style. There are some text escape codes. As you quoted the \n that breaks the line.

Other escape characters:

  • \b: Backspace (U+0008 BACKSPACE)
  • \f: New form (U+000C FORM FEED)
  • \n: New line (U+000A LINE FEED)
  • \r: Car return (U+000D CARRIAGE RETURN)
  • \t: Horizontal Tabulation (U+0009 CHARACTER TABULATION)
  • \v: Tabulation Vertical(U+000B LINE TABULATION)
  • \0: Nullity (U+0000 NULL)

function ola_b(){var str = 'Hello \bWorld'; alert(str); console.log(str);};
function ola_f(){var str = 'Hello \fWorld'; alert(str); console.log(str);};
function ola_n(){var str = 'Hello \nWorld'; alert(str); console.log(str);};
function ola_r(){var str = 'Hello \rWorld'; alert(str); console.log(str);};
function ola_t(){var str = 'Hello \tWorld'; alert(str); console.log(str);};
function ola_v(){var str = 'Hello \vWorld'; alert(str); console.log(str);};
function ola_0(){var str = 'Hello \0World'; alert(str); console.log(str);};
<button onclick="return ola_b()">Alert \b</button>
<button onclick="return ola_f()">Alert \f</button>
<button onclick="return ola_n()">Alert \n</button>
<button onclick="return ola_r()">Alert \r</button>
<button onclick="return ola_t()">Alert \t</button>
<button onclick="return ola_v()">Alert \v</button>
<button onclick="return ola_0()">Alert \0</button>


This also depends on the javascript engine of the browser, I did some tests and the messages were better presented in IE11 than in Chrome42.


I confess that several I never used, nor I know for what it serves.

You can read more about them here en.

  • Here (Chrome, OSX), the last 2 examples show "Hello World", as if the control character did not exist.

  • Yes @bfavaretto, I made an edition there saying that IE11 interprets the codes better than Chrome. The last \0 it disappears with the string, the penultimate Chrome breaks the line and IE11 does nothing...

3

The alert just displays the given string, without formatting anything. What happens is that javascript itself interprets some special characters in a string. The most common are \n (line breaking) and \t (tabulation), but is not restricted to this.

You can even use any UTF-8 character using \uX, where X is the code of the desired character, for example, \u2605 (★).

Because this behavior is not connected to alert and yes the language (all languages I know support special characters), you can use this behavior in other functions as well, as in console.log.

alert("teste \u2605\n\tlegal");

console.log("teste \u2605\n\tlegal");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.