By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,517 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,517 IT Pros & Developers. It's quick & easy.

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

P: 3
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

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

Expand|Select|Wrap|Line Numbers
  1. //Created by Fader Maker (http://www.bosiljak.hr/fadermaker/)
  2. var fmTimer = null;
  3. var fmfg = false;
  4. function fminit(){
  5. if (!document.getElementById) return;
  6. fma1=0xff; fma2=0x00; fma3=0xff;
  7. fma4=0xff; fma5=0x00; fma6=0x00;
  8. if (!fmfg){
  9. fmtemp=fma1; fma1=fma4; fma4=fmtemp; fmtemp=fma2; fma2=fma5; fma5=fmtemp; fmtemp=fma3; fma3=fma6; fma6=fmtemp;
  10. }
  11. fmfg = !fmfg;
  12. fmFade();
  13. }
  14. function fmToHex(n){
  15. var hexChars = "0123456789ABCDEF";
  16. if (n == 0) return "00";
  17. var j, n;
  18. var temp = "";
  19. while (n != 0){
  20. j = n % 16;
  21. n = (n - j)/16;
  22. temp = hexChars.charAt(j) + temp;
  23. }
  24. if (temp.length < 2){
  25. temp = "0" + temp;
  26. }
  27. return temp;
  28. }
  29.  
  30. function fmFade(){
  31. obj = document.getElementById('tekst').style;
  32. fmcolor = "#" + fmToHex(fma1) + fmToHex(fma2) + fmToHex(fma3);
  33. obj.color = fmcolor;
  34. if (fma1 != fma4){
  35. if (fma4 > fma1){fma1++;}
  36. else{fma1--;}}
  37. if (fma2 != fma5){
  38. if (fma5 > fma2){fma2++;}
  39. else{fma2--;}}
  40. if (fma3 != fma6){
  41. if (fma6 > fma3){fma3++;}
  42. else{fma3--;}}
  43. if ((fma1 == fma4) && (fma2 == fma5) && (fma3 == fma6)){
  44. window.clearTimeout(fmTimer);
  45. fminit()}
  46. else{
  47. fmTimer = window.setTimeout("fmFade()",0);
  48. }
  49. }
  50. 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.
Dec 9 '09 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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.
Dec 10 '09 #2

P: 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.
Dec 10 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
I haven't tried it in the external file, is that a possibility?
thatís where you would normally place it.
Dec 10 '09 #4

P: 3
that’s where you would normally place it.
I tried it, and still no luck.
Dec 11 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.