Change CSS property onload

Discussion in 'Javascript' started by mIDO, Mar 28, 2007.

  1. mIDO

    mIDO Guest

    Hello,

    I have developed a script that change the font size in css, but only
    the attribute "font-size" on the body tag, not change the entire
    active stylesheet.

    When i click to change the size
    <a href="#" title="Change Font Size"
    onclick="cambia_font_size(2);">A</a>

    It works fine, but when i try to check the cookie to set the default
    font size, it does not work.

    The cookie stores the value correctly and i can read the value. The
    problem is when i try to change the value:
    document.getElementsByTagName("body")[0].style.fontSize=tam;

    I have tried also writing this code at the end of the html document
    but it doesnt work
    <script type="text/javascript">inicializa_fontsize();</script>

    What could happen?

    Thanks


    CODE
    ----------------------------
    window.onload = function inicializa_fontsize() {
    var valor=readCookie('fontsizeptgc');

    if(valor!=null) {
    //alert(valor);
    cambia_font_size(valor); //cambia valor al hacer click pero no
    "onload". revisar
    }
    }

    function cambia_font_size(valor) {
    var tam;
    switch(valor) {
    case 1:
    tam='9px';
    break;
    case 2:
    tam='11px';
    break;
    case 3:
    tam='12px';
    break;
    default:
    tam='11px';
    break;
    }

    document.getElementsByTagName("body")[0].style.fontSize=tam;
    createCookie('fontsizeptgc',valor,'7');
    }

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

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca;
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return
    c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    function eraseCookie(name) {
    createCookie(name,"",-1);
    }
     
    mIDO, Mar 28, 2007
    #1
    1. Advertising

  2. mIDO

    mIDO Guest

    Edited >>

    It works fine, but when i try to check the cookie to set the default
    font size AT ONLOAD, it does not work.
    ----
     
    mIDO, Mar 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    807
    Fabio R.
    Mar 25, 2005
  2. Joshua Beall
    Replies:
    1
    Views:
    443
    Bertilo Wennergren
    Dec 10, 2003
  3. Noozer
    Replies:
    10
    Views:
    2,129
    Mitja
    Oct 13, 2004
  4. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    566
    Martin Honnen
    Nov 4, 2004
  5. Replies:
    5
    Views:
    270
    Thomas 'PointedEars' Lahn
    May 15, 2005
Loading...

Share This Page