Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Tags in Javascript Alert() method

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

<b> <ul> <li> 

etc.

Thanks for your help

like image 375
OrahSoft Avatar asked Mar 11 '11 20:03

OrahSoft


People also ask

What does the alert () function in JavaScript do?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

How do I show an alert in HTML?

The Window alert() method is used to display an alert box. It displays a specified message along with an OK button and is generally used to make sure that the information comes through the user. It returns a string which represents the text to display in the alert box.

Which object has alert () method in JavaScript?

The alert() is a method of the window object.

How do I get alerts in JavaScript?

The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.


2 Answers

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

document.getElementById("test").onclick = function() {    alert(      '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.

like image 109
fuxia Avatar answered Oct 05 '22 23:10

fuxia


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

like image 20
Naftali Avatar answered Oct 05 '22 21:10

Naftali