I tried to convert this Javascript to an external .js file, but it won't work...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hyperman7
    New Member
    • Dec 2009
    • 3

    I tried to convert this Javascript to an external .js file, but it won't work...

    I saved the code below to an external file which I called colorFader.js, and I'm calling it with:
    <script type="text/javascript" src="js/colorFader.js"> </script>

    My path (above) is correct, so I'm guessing its something to do with the "onload" command at the end of the code. The code works just fine if I paste it into the <head> tag of my web page adding the standard javascript open and close <script> tags, but as soon as I save it as an external file, it breaks. If anyone could help out as to how to fix this, I would sure appreciate it, as I need to put this on several hundred pages. Thanks

    -------------------------------------

    Code:
    //Created by Fader Maker ([url]http://www.bosiljak.hr/fadermaker/[/url])
    var fmTimer = null;
    var fmfg = false;
    function fminit(){
    if (!document.getElementById) return;
    fma1=0xff; fma2=0x00; fma3=0xff;
    fma4=0xff; fma5=0x00; fma6=0x00;
    if (!fmfg){
    fmtemp=fma1; fma1=fma4; fma4=fmtemp; fmtemp=fma2; fma2=fma5; fma5=fmtemp; fmtemp=fma3; fma3=fma6; fma6=fmtemp;
    }
    fmfg = !fmfg;
    fmFade();
    }
    function fmToHex(n){
    var hexChars = "0123456789ABCDEF";
    if (n == 0) return "00";
    var j, n;
    var temp = "";
    while (n != 0){
    j = n % 16;
    n = (n - j)/16;
    temp = hexChars.charAt(j) + temp;
    }
    if (temp.length < 2){
    temp = "0" + temp;
    }
    return temp;
    }
    
    function fmFade(){
    obj = document.getElementById('tekst').style;
    fmcolor = "#" + fmToHex(fma1) + fmToHex(fma2) + fmToHex(fma3);
    obj.color = fmcolor;
    if (fma1 != fma4){
    if (fma4 > fma1){fma1++;}
    else{fma1--;}}
    if (fma2 != fma5){
    if (fma5 > fma2){fma2++;}
    else{fma2--;}}
    if (fma3 != fma6){
    if (fma6 > fma3){fma3++;}
    else{fma3--;}}
    if ((fma1 == fma4) && (fma2 == fma5) && (fma3 == fma6)){
    window.clearTimeout(fmTimer);
    fminit()}
    else{
    fmTimer = window.setTimeout("fmFade()",0);
    }
    }
    onload = fminit
    ---------------------------------------------------------

    The HTML part of the code in the body consists of adding an <span id="tekst" > tag around the text I want to effect. </span>

    Also I tried taking off the "onload = fminit" in the external file, but no luck, and I get no error codes either way.
    Last edited by Dormilich; Dec 10 '09, 06:58 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    have you already tried window.onload = fminit;?

    you should also set breakpoints (at least FF, Safari and (I think) Opera provide debugging tools) to see, if your functions execute.

    Comment

    • hyperman7
      New Member
      • Dec 2009
      • 3

      #3
      Yes, I tried that in the body tag of the webpage where I am calling the script, it doesn't seem to make any difference, I haven't tried it in the external file, is that a possibility? thanks for the suggestion. I'm not familiar with setting break points, however it doesn't work in any browser when called as an external .js file.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I haven't tried it in the external file, is that a possibility?
        that’s where you would normally place it.

        Comment

        • hyperman7
          New Member
          • Dec 2009
          • 3

          #5
          that’s where you would normally place it.
          I tried it, and still no luck.

          Comment

          Working...