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

javascript - Tab 't' in alert box not working with Chrome - Stack Overflow

programmeradmin5浏览0评论

I want to display three lines of text in a Javascript alert box with center alignment of text.

I'm using the following code for that,

alert(
    '\t\t\t\t'+"Congratulations!" + '\n\t' +
    "You are now subscribed with test!" + '\n' +
    "Keep on eye out on your inbox for future updates from us!"
);

It's working fine with Firefox. But in chrome, the tab (\t) character is not working. Texts are left aligned in all lines. Please help.

I want to display three lines of text in a Javascript alert box with center alignment of text.

I'm using the following code for that,

alert(
    '\t\t\t\t'+"Congratulations!" + '\n\t' +
    "You are now subscribed with test.!" + '\n' +
    "Keep on eye out on your inbox for future updates from us!"
);

It's working fine with Firefox. But in chrome, the tab (\t) character is not working. Texts are left aligned in all lines. Please help.

Share Improve this question edited May 17, 2016 at 19:01 Cave Johnson 6,7785 gold badges41 silver badges61 bronze badges asked Jul 11, 2012 at 5:03 designersvsoftdesignersvsoft 1,8599 gold badges39 silver badges66 bronze badges 2
  • 4 You really shouldn't rely on text positioning in alert() boxes. You also really shouldn't use an alert box for telling someone they're subscribed. Just put a message on the page. – Amber Commented Jul 11, 2012 at 5:05
  • 1 alertbox is waaaaaay aged man ! try other cool things like colorbox jacklmoore./colorbox and also alertbox is a part of the browser ,which has minimal style functionalities – coolguy Commented Jul 11, 2012 at 5:07
Add a ment  | 

3 Answers 3

Reset to default 5

As a workaround, you can use several spaces instead. E.g...

<script>
    alert('Food:\n   Apples\n   Bread\n   Carrots');
</script>

A working example:

$(function(){
  $('#btnShowAlert').on('click', function(){
    alert('Food:\n   Apples\n   Bread\n   Carrots');
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnShowAlert">Show Alert</button>

Seems to have been an issue for awhile sadly :(

http://productforums.google./forum/#!topic/chrome/bfmvqAvtSd4

Found that and it makes it out to look like it's not possible.

Perhaps use something that'd mimic an alert box in appearance?

This works for me:

alert("Name: \u00A0Jose \u00A0Garcia");

\u00A0 is the non-breaking space character. I know it's not a tab per se, but \t gives me a weird line before the text like this: -Text, so I ended up using \u00A0 instead. Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论