Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Beyond Lament
    Posts
    424
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is it possible to center text in an Alert box?

    I know the \n trick, but is there a way to center the text that's in an alert box?
    Quíet Storm Designs ~ Art is not what you see, but what you make others see.
    · the Storms· || ·Ultraviolent Winter· || ·Was Einstein Wrong?· || ·It´s About Time!·

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Not really. If you know the amount of characters until it wraps, then possibly, but testing in Moz 1.1b+, the alert box's width grows to accomadate text on one line.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I think &nbsp &nbsp will do the trick.

    That was supposed to be &nobreakspace &nobreakspace

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i use \t, which is a tab. i figure out how many characters i'm going to use on a line, then encase the text in \t 's.

    eg:


    \tThis text will be centered\t\n
    \tin the middle of the alert\t

    it's not perfect, but it's as close as i ever got.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    Regular Coder
    Join Date
    May 2002
    Location
    Maine, USA
    Posts
    573
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Joh6nn, just to avoid confusion, I deleted my post while you were posting because I decided it was an invalid solution and not worth keeping in place .
    boxer_1
    CodingForums Moderator
    "How did a fool and his money get together in the first place?"

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It works. But it doesn't look like it does!

    Below is the code I was playing with. My conclusion... The non-fixed pitch font used won't allow the text to properly center. Is it possible to change the font type to courier?

    P.S. I used a "." instead of " " so I could see where blanks are being inserted

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    <html>
    <head>
    <title>Centering a Message in an Alert Box</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function cutString (theString, lineWidth) {
       var endindex = lineWidth;
       var MessageFrag = new String();
       var RemainingFrag = new String();
       var padding = 0;
         
       if (theString.length <= lineWidth) {
          MessageFrag = theString;
          padding = Math.ceil((lineWidth - MessageFrag.length) / 2);
    
          for (var i=1; i<= padding; i++) {
             MessageFrag = " " + MessageFrag;
          }
    
          alert ("hit bottom!. Return now");
          return (MessageFrag);
       }
       
       while (theString.charAt(endindex) != " ") {
          endindex--;
       }
       MessageFrag   = theString.substring(0,endindex);
       RemainingFrag = theString.substring(endindex+1, theString.length);   
       padding = Math.ceil((lineWidth - MessageFrag.length) / 2);
    
       for (var i=1; i<= padding; i++) {
          MessageFrag = "." + MessageFrag;
       }
       MessageFrag += "\n" + cutString(RemainingFrag, lineWidth);
       alert ("returning with:\n" + MessageFrag);
       return (MessageFrag);
       
    } // fuction cutString();
    //-->
    
    var message = new String("Here is a message that I want displayed, centered inside of an alert box. Will it work!?");
    var lineLength = 25;
    var centeredString = new String();
    
    alert ("Here is the original string: \n" + message + "\nlinelength = " + lineLength);
    centeredString = cutString(message, lineLength);
    alert ("Here it is centered:\n" + centeredString);
    
    </script>
    
    </head>
    <body>
    
    </body>
    </html>

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Beyond Lament
    Posts
    424
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by joh6nn
    i use \t, which is a tab. i figure out how many characters i'm going to use on a line, then encase the text in \t 's.

    eg:


    \tThis text will be centered\t\n
    \tin the middle of the alert\t

    it's not perfect, but it's as close as i ever got.
    That works for me! Thanks!
    Quíet Storm Designs ~ Art is not what you see, but what you make others see.
    · the Storms· || ·Ultraviolent Winter· || ·Was Einstein Wrong?· || ·It´s About Time!·


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •