Javascript alert settings

8

Are there other ways to change the alert display? for example when I use \n it breaks the line, I would like to know if there are other commands of which 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 had this same curiosity about alert()

Most interesting example

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

Note that Print plugin is loaded but NOT active! text gets different formatting

    
asked by anonymous 28.04.2015 / 16:17

2 answers

4

alert is a "pure" text window, meaning markup languages will not work, let alone style. There are some text escape codes. As you mentioned, 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 Tab (U + 0009 CHARACTER TABULATION)
  • \v : Vertical Tab (U + 000B LINE TABULATION)
  • %code% : Null character (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 
<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 
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 
<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 %pre%</button>
World'; alert(str); console.log(str);};
</button>
World'; alert(str); console.log(str);};
%pre%

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

I confess that several I have never used, nor do I know what it is for.

You can read more about them here in .

    
28.04.2015 / 16:33
3

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

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

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

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

console.log("teste \u2605\n\tlegal");
    
28.04.2015 / 16:45