最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I put formatted HTML code as Javascript alert() input parameter? - Stack Overflow

programmeradmin1浏览0评论

I have this simpe Javascript script that show a popup box when a link is clicked:

    <script>
        function myExample() {
            alert("BaZing!  It works!");
        }
    </script>

I want to know if it is possible to format the text inside the alert() function.

In particular I want to know if (and how) is it possible to put some formatted HTML text as alert() input parameter.

Some one can help me?

I have this simpe Javascript script that show a popup box when a link is clicked:

    <script>
        function myExample() {
            alert("BaZing!  It works!");
        }
    </script>

I want to know if it is possible to format the text inside the alert() function.

In particular I want to know if (and how) is it possible to put some formatted HTML text as alert() input parameter.

Some one can help me?

Share Improve this question edited Oct 5, 2013 at 16:37 furins 5,0481 gold badge42 silver badges58 bronze badges asked Oct 5, 2013 at 16:33 AndreaNobiliAndreaNobili 43.1k122 gold badges364 silver badges682 bronze badges 2
  • As long as it's a string, you can put anything you'd like in there? HTML and styles doesn't work on OS/browser specific windows, obviously! – adeneo Commented Oct 5, 2013 at 16:34
  • You can't, use a custom modal. – alex Commented Oct 5, 2013 at 16:34
Add a ment  | 

2 Answers 2

Reset to default 3

No, alert() accepts one string argument and will display it as is.

If you need to display a formatted text in a modal, use a library (e.g. Alertify.js).

alert() can't format a HTML format string, but if you just want separate a string to several lines, you can put '\n' in your string,like

alert("first line \n second line \n third line");

发布评论

评论列表(0)

  1. 暂无评论