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

javascript - Chrome rendering an alert() box with the last 2 words on 2 separate lines. How to fix? - Stack Overflow

programmeradmin6浏览0评论

I have the following JS that produces an alert box:

alert("You have selected the maximum number of funds available for parison. Please ensure only five funds have been selected for your basket");

Chrome is rendering the alert box like this:

with the last 2 words wrapping onto separate lines.

Has anyone seen this before? Any ideas how to fix this?!


Wanted to add another screenshot since I am able to reproduce this error: --Jeremy

I have the following JS that produces an alert box:

alert("You have selected the maximum number of funds available for parison. Please ensure only five funds have been selected for your basket");

Chrome is rendering the alert box like this:

with the last 2 words wrapping onto separate lines.

Has anyone seen this before? Any ideas how to fix this?!


Wanted to add another screenshot since I am able to reproduce this error: --Jeremy

Share edited Jun 13, 2011 at 14:02 Jeremy 22.4k4 gold badges70 silver badges81 bronze badges asked Jun 13, 2011 at 13:13 DaveDevDaveDev 42.2k73 gold badges232 silver badges395 bronze badges 9
  • Bizarre; it's not doing that to me with your "alert()", but I'm using Chrome on Linux so maybe that makes a difference. – Pointy Commented Jun 13, 2011 at 13:16
  • Works ok for me, chrome 12, windows. – Alex K. Commented Jun 13, 2011 at 13:25
  • This is extremely odd. I can confirm this with Chrome 11 on XP. Any words I put after "basket" seem to flow normally, yet the line break after "your" persists. – Jeremy Commented Jun 13, 2011 at 13:27
  • @Jeremy, This image was actually supplied by the client, and I think she's using Chrome 8. I'm using 11 on Windows 7, and it works fine for me.. It's interesting that you're on XP with version 11 and you're seeing this also. I think the culprit is Chrome on XP with the word 'basket'? (for some odd reason).. at least I have something to go with now. (though where, I don't know!) – DaveDev Commented Jun 13, 2011 at 13:40
  • @DaveDev - get your client to update their Chrome version -- v12 has been released, so it should update for them automatically. If it's a Chrome bug there's a good chance it'll magically e right after the update. – Spudley Commented Jun 13, 2011 at 13:54
 |  Show 4 more ments

5 Answers 5

Reset to default 5

This seems to be a bug with older verisons of Chrome.

http://code.google./p/chromium/issues/detail?id=83670

In Chrome 12 on linux it works fine, so I can't easily reproduce/debug. Have you tried putting in your own line breaks, say after every 70 characters like:

alert("You have selected the maximum number of funds \n"+
      "available for parison. Please ensure only five \n"+
      "funds have been selected for your basket.");

I understand hard-coding linebreaks is a bad idea in general (in some cases they will be poor choices) or if the text ever changes the line breaks will have to be adjusted by you.

As a programmer/developer you can't fully account for chrome not working properly. Except maybe avoiding alert box entirely, and sending the error message another way (e.g., ajax or a custom alert from say jquery).

@DaveDev, here is the image you requested. I think it looks good enough.

The code is from @dr jimbob's answer.

I remend to you that you, as a programmer, controll the length of each string. You can cut the line, by yourself, with a '\n' character, and control every line for yourself.

I suspect there is a line end in your document, because testing it in my Chrome (12, windows 7), the alert displays like it should. Are you using HTMLTidy or some HTML formatter?

[edit] Okay, seems to be a genuine Chrome issue. Here some related issues @code.google. for Chrome

  • Lengthy alerts have weird line break in them
  • Long text messages get truncated on the Javascript Alert box
  • javascript alert popup truncates strings every 132 chars

The solution I have stumbled upon is to add +"\ " (backslash space) at the end of the string. Each of my text lines ends with a \n in any case, so effectively my string now ends with "\n\ "Then it seems I can use as many \n's that I want. Don't ask me why it works. Perhaps it will work for you? Just call me persistent!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论