HTML Tags in Javascript Alert() method

I wanted to know if it's possible to add HTML tags to JavaScript alert() method, such as:



Thanks for your help



You can use all Unicode characters and the escape characters \n and \t. An example:

document.getElementById("test").onclick = function() {
    'This is an alert with basic formatting\n\n' 
    + "\t• list item 1\n" 
    + '\t• list item 2\n' 
    + '\t• list item 3\n\n' 
    + '???????????????????????\n\n' 
    + 'Simple table\n\n' 
    + 'Char\t| Result\n' 
    + '\\n\t| line break\n' 
    + '\\t\t| tab space'
<!DOCTYPE html>
<title>Alert formatting</title>
<meta charset=utf-8>
<button id=test>Click</button>

Result in Firefox:

screenshot Firefox

You get the same look in almost all browsers.


alert() is a method of the window object that cannot interpret HTML tags


You can add HTML into an alert string, but it will not render as HTML. It will just be displayed as a plain string. Simple answer: no.


This is not possible.

Instead, you should create a fake window in Javascript, using something like jQuery UI Dialog.


No, you can use only some escape sequences - \n for example (maybe only this one).


alert() doesn't support HTML, but you have some alternatives to format your message.

You can use Unicode characters as others stated, or you can make use of the ES6 Template literals. For example:

.catch(function (error) {
  const alertMessage = `Error retrieving resource. Please make sure:
    • the resource server is accessible
    • you're logged in

    Error: ${error}`;

Output: enter image description here

As you can see, it maintains the line breaks and spaces that we included in the variable, with no extra characters.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.