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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Can anyone find a bug in this or a way to improve it ?



    function textSize(updown) {
    var size = Number(readCookie('user-font-size'));
    if (size == 0) {
    size = 12; // No Cookie, Set A Default Size Of 12px
    }
    size = size + Number(updown); // Adjust The Current Size
    if (size > 20) { size = 20; } // Stop At 13
    if (size < 12) { size = 12; } // Stop At 10
    writeCookie('user-font-size', size, 12); // Update The Cookie
    document.body.style.fontSize = size + 'px'; // Set The Users Text Size
    }


    function writeCookie(name, value, days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = '; expires=' + date.toGMTString();
    } else {
    expires = '';
    }
    document.cookie = name + '=' + value + expires;
    }

    function readCookie(name) {
    name += '=';
    var cs = document.cookie.split(';');
    for (var i = 0; i < cs.length; i++) {
    var c = cs[i];
    while (c.charAt(0) == ' ') {
    c = c.substring(1, c.length);
    }
    if (c.indexOf(name) == 0) { //-----------//
    return c.substring(name.length, c.length); // Gotcha //
    } //-----------//
    } //------------//
    return null; // Failed //
    } //------------











    <img id="plustext" class="zoom-in" alt="Increase text size" src="Zoom-In-icon.png" onclick="textSize (2);" style="cursorointer" />
    <img id="minustext" class="zoom-out" alt="Decrease text size" src="Zoom-Out-icon.png" onclick="textSize (-2);" style="cursorointer" />


    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel fringilla arcu. Cras ullamcorper nunc ac sapien gravida ultrices.
    Curabitur mollis consectetur elit non tempus. Duis blandit luctus feugiat. Nullam eget tellus at quam dapibus tempus in scelerisque ligula.
    Phasellus turpis ante, tincidunt at aliquam vel, luctus vitae leo. Suspendisse dignissim vehicula est, ac dapibus justo pellentesque eu.
    </p>






    cheers hope thats enough code

    Ant.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Well, if you are going to read multiple cookie values in the same page, then having to go back into the cookie string to get each one, one at a time, is inefficient. But not horribly so.

    And I'd probably check the cookie value for null, rather than just assuming I can convert it to Number().

    Code:
    function textSize(updown) 
    {
        var size = readCookie('user-font-size');
        if ( ( size = size==null ? 0 : Number(size) ) == 0) {
        ...
    p.s.: Please use [code] tags in postings instead of [icode] tags. [icode] tags are only useful for up to maybe one line of code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Cool thanks for the pointer there.

    currently this piece of script only re sizes everything in the "body" tag, how could change this script to only re size a specific tag for example a <span></span>


    any ideas?

    cheers,
    Ant.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Code:
    function textSize(updown) {
        ...
        writeCookie('user-font-size', size, 12); // Update The Cookie
        var changes = document.getElementsByClassName("resizable");
        for ( var c = 0; c < changes.length; ++c )
        {
             changes[c].style.fontSize = size + 'px'; 
        }
    }
    That won't work for older MSIE browsers, as they don't support getElementsByClassName.

    So you could do:
    Code:
    function textSize(updown) {
        ...
        writeCookie('user-font-size', size, 12); // Update The Cookie
        var spans = document.getElementsByTagName("span");
        for ( var c = 0; c < changes.length; ++c )
        {
             if ( spans[c].className == "resizable" ) spans[c].style.fontSize = size + 'px'; 
        }
    }
    And many variations on this theme.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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