Include js files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jfancy-Transport Canada

    #1

    Include js files

    Hi, I am making a js file that increases font sizes by its relative
    size. It works fine as all one file with the script in the html file
    but when i go to put the js in a seperate file and reference it as a
    source file, it doesn't work properly. What happens is that when i go
    to increase the font size, it goes lower(the first time), but the next
    time it increase and increases, and decreases, which is fine. But
    whatever happens, it sets it to a different start value, which is
    wrong.

    here's the code for the html file:
    -------------------------------------------------------------------
    <html>
    <head>
    <SCRIPT TYPE="text/javascript" SRC="textsize2. js"></SCRIPT>
    </head>
    <body>

    <h1>Hello there</h1>
    <h2>Test</h2>
    anything.

    <input type="button" onclick="resize BodyText(2, 'n')" value="Font +" >
    <input type="button" onclick="resize BodyText(-2, 'n')" value="Font -" >

    <input type="reset" onclick= "resizeBodyText (0, 'y')">

    </body>
    </html>
    ------------------------------------------------------------------



    And here's the code for the js file:

    -------------------------------------------------------------------
    var current = parseInt(getCoo kie("fontFactor "))
    if (isNaN(current) )
    current= 0;

    resizeBodyText( current, "n")
    function resizeBodyText( factor, reset)
    {
    if (reset=="y")
    factor= (current * -1);

    window.alert(cu rrent + " " + factor)
    //------------------------------------------------
    var a = document.all;
    var s = '';
    current += factor;

    if (current < 0)
    current = 0;
    else
    for (var i = a.length-1; i >0;i--)
    {
    s=a[i].currentStyle.f ontSize+'';
    s=Right(s,2);
    a[i].style.fontSize = parseInt(a[i].currentStyle.f ontSize)+factor +s;
    }
    setCookie("font Factor", current)
    }

    //-----------------------------------------------
    function Right(str, n) {
    if (n <= 0)
    return "";
    else if (n > String(str).len gth)
    return str;
    else {
    var iLen = String(str).len gth;
    return String(str).sub string(iLen, iLen - n);
    }
    }
    //-------------------------------------------------
    function getCookie(name) {
    var dc = document.cookie ;
    var index = dc.indexOf(name + "=");
    if (index == -1) return null;
    index = dc.indexOf("=", index) + 1; // first character
    var endstr = dc.indexOf(";", index);
    if (endstr == -1) endstr = dc.length; // last character
    return unescape(dc.sub string(index, endstr));
    }
    function setCookie(name, value)
    {
    document.cookie = name + "=" + escape(value);
    }
    -------------------------------------------------------------------

    If someone could help me, it would be greatly appreciated! Thanks

    JF

  • RobG

    #2
    Re: Include js files

    jfancy-Transport Canada wrote:[color=blue]
    > Hi, I am making a js file that increases font sizes by its relative
    > size. It works fine as all one file with the script in the html file
    > but when i go to put the js in a seperate file and reference it as a
    > source file, it doesn't work properly. What happens is that when i go
    > to increase the font size, it goes lower(the first time), but the next
    > time it increase and increases, and decreases, which is fine. But
    > whatever happens, it sets it to a different start value, which is
    > wrong.
    >[/color]

    It seems pointless to replicate a function that is provided by a
    browser in a much more efficient manner than you can do, but let's have
    fun anyway.

    I ignored the cookie stuff.
    [color=blue]
    > here's the code for the html file:
    > -------------------------------------------------------------------
    > <html>
    > <head>
    > <SCRIPT TYPE="text/javascript" SRC="textsize2. js"></SCRIPT>
    > </head>
    > <body>
    >
    > <h1>Hello there</h1>
    > <h2>Test</h2>
    > anything.
    >
    > <input type="button" onclick="resize BodyText(2, 'n')" value="Font +" >
    > <input type="button" onclick="resize BodyText(-2, 'n')" value="Font -" >
    >
    > <input type="reset" onclick= "resizeBodyText (0, 'y')">
    >
    > </body>
    > </html>
    > ------------------------------------------------------------------
    >
    >
    >
    > And here's the code for the js file:
    >
    > -------------------------------------------------------------------
    > var current = parseInt(getCoo kie("fontFactor "))[/color]

    You should always use parseInt with a radix. In this case, probably
    better to use parseFloat anyway (font size units may be decimal). I
    just set current to 0 and removed the cookie stuff.
    [color=blue]
    > if (isNaN(current) )
    > current= 0;
    >
    > resizeBodyText( current, "n")[/color]

    This will attempt to run your resizeBodyText function probably before
    the document has finished loading. Run it from an onload (either in
    the HTML source or using window.onload).
    [color=blue]
    > function resizeBodyText( factor, reset)
    > {
    > if (reset=="y")
    > factor= (current * -1);
    >
    > window.alert(cu rrent + " " + factor)
    > //------------------------------------------------
    > var a = document.all;[/color]

    This will only work in browsers that support document.all, so feature
    test first:

    if (document.all) {
    var a = document.all;
    } else {
    return; // No point in continuing
    }

    or add an alternative if you add cross-browser support for
    currentStyle:

    if (document.getEl ementsByTagName ) {
    var a = document.getEle mentsByTagName( '*');
    } else if (document.all) {
    var a = document.all;
    } else {
    return; // No point in continuing
    }
    [color=blue]
    > var s = '';
    > current += factor;
    >
    > if (current < 0)
    > current = 0;
    > else[/color]

    ?
    Don't understand the logic of the if..else. Why not just the
    following 'for'?
    [color=blue]
    > for (var i = a.length-1; i >0;i--)
    > {
    > s=a[i].currentStyle.f ontSize+'';
    > s=Right(s,2);
    > a[i].style.fontSize = parseInt(a[i].currentStyle.f ontSize)+factor +s;
    > }[/color]

    This seems cumbersome. Try this, it eliminates Right() and tests for
    currentStyle & style support before trying to use them:

    // exit if currentStyle or style not supported
    if (!a[0].currentStyle || !a[0].style ) return;

    var s, an, au, i=a.length;
    while ( --i ) {
    s=a[i].currentStyle.f ontSize;
    an = parseFloat(s); // Get the number part
    au = s.replace(an,'' ); // Get the units
    a[i].style.fontSize = an + factor + au;
    }

    Note that use of currentStyle is likely IE only (certainly won't work
    in Firefox).

    If you want to add support for other browsers, do a search in the news
    group for getComputedStyl e - you will likely want to create your own
    function that uses one or the other depending on browser support.

    You may realise that the above avoids a[0], which is the HTML
    element. Setting the font size there will give all elements the same
    size font (this may not have been intended originally, but that's what
    happens).
    [color=blue]
    > setCookie("font Factor", current)
    > }
    >
    > //-----------------------------------------------
    > function Right(str, n) {
    > if (n <= 0)
    > return "";
    > else if (n > String(str).len gth)
    > return str;
    > else {
    > var iLen = String(str).len gth;
    > return String(str).sub string(iLen, iLen - n);
    > }
    > }[/color]

    No longer required

    [...]
    [color=blue]
    > If someone could help me, it would be greatly appreciated! Thanks
    >[/color]

    The buttons should only appear in browsers that support the features of
    your script, so maybe test that right up front and only show them if
    they're going to work.

    The resize function is now:

    function resizeBodyText( factor, reset) {
    if (reset=="y"){
    factor = (current * -1);
    }

    current += factor;

    if (document.getEl ementsByTagName ) {
    var a = document.getEle mentsByTagName( '*');
    } else if (document.all) {
    var a = document.all;
    } else {
    return; // No point in continuing
    }

    // exit if currentStyle or style not supported
    if (!a[0].currentStyle || !a[0].style ) return;

    var s, as, au, i = a.length;
    while ( --i ) {
    s = a[i].currentStyle.f ontSize;
    an = parseFloat(s); // Get the number part
    au = s.replace(an,'' ); // Get the units
    a[i].style.fontSize = an + factor + au;
    }

    setCookie("font Factor", current)

    }




    --
    Rob

    Comment

    • RobG

      #3
      Re: Include js files

      RobG wrote:[color=blue]
      > jfancy-Transport Canada wrote:
      >[color=green]
      >> Hi, I am making a js file that increases font sizes by its relative
      >> size. It works fine as all one file with the script in the html file
      >> but when i go to put the js in a seperate file and reference it as a
      >> source file, it doesn't work properly. What happens is that when i go
      >> to increase the font size, it goes lower(the first time), but the next
      >> time it increase and increases, and decreases, which is fine. But
      >> whatever happens, it sets it to a different start value, which is
      >> wrong.
      >>[/color]
      >
      > It seems pointless to replicate a function that is provided by a
      > browser in a much more efficient manner than you can do, but let's have
      > fun anyway.
      >[/color]
      [...]
      [color=blue]
      > Note that use of currentStyle is likely IE only (certainly won't work
      > in Firefox).
      >
      > If you want to add support for other browsers, do a search in the news
      > group for getComputedStyl e - you will likely want to create your own
      > function that uses one or the other depending on browser support.
      >[/color]

      Below is a cross-browser version, tested in IE 6 and Firefox. One
      issue is that it loads, then does the re-size so in larger pages there
      will be some issues when the page re-sizes. Highlights that this is
      not a good idea.



      <!-- new body tag in HTML -->

      <body onload="resizeB odyText('onload ','n')">



      /* Revised script */

      // Setup cross browser functions
      // Get all elements
      var getAll;
      var getFontSize;

      function initFunctions() {
      if (document.getEl ementsByTagName ) {
      getAll = function() {return document.getEle mentsByTagName( '*')};
      } else if (document.all){
      getAll = function() {return document.all};
      } else {
      getAll = function() {return false};
      }

      // Get element font size
      var x = document.body;
      if ( x.currentStyle ) {
      getFontSize = function(el) {
      return el.currentStyle .fontSize;
      }
      } else if ( document.defaul tView.getComput edStyle(x,'').f ontSize ){
      getFontSize = function(el) {
      return document.defaul tView.getComput edStyle(el,''). fontSize;
      }
      } else {
      getFontSize = function(el) {
      return false;
      }
      }
      }

      // Resize text
      function resizeBodyText( factor, reset) {
      if (reset=="y"){
      factor -= current;
      }

      if ( 'onload' == factor ) {
      factor = current;
      } else {
      current += +factor;
      }

      var a = getAll();

      // exit if features not supported
      if (!a || !getFontSize(a[0]) || !a[0].style ) return;

      var s, as, au, i = a.length;
      while ( --i ) {
      s = getFontSize(a[i]); // Get the font size
      an = parseFloat(s); // Get the number part
      au = s.replace(an,'' ); // Get the units
      a[i].style.fontSize = an + factor + au;
      }
      setCookie("font Factor", current)
      }

      //-------------------------------------------------
      function getCookie(name) {
      var dc = document.cookie ;
      var index = dc.indexOf(name + "=");
      if (index == -1) return null;
      index = dc.indexOf("=", index) + 1; // first character
      var endstr = dc.indexOf(";", index);
      if (endstr == -1) endstr = dc.length; // last character
      return unescape(dc.sub string(index, endstr));
      }
      //-------------------------------------------------
      function setCookie(name, value)
      {
      document.cookie = name + "=" + escape(value);
      }

      // Get the factor from the cookie
      var current = +getCookie("fon tFactor");
      if ( isNaN(current) ){
      current = 0;
      }




      --
      Rob

      Comment

      Working...