473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic "shiny" link mouseover script produces non-fatal errors.

65 New Member
Hey everyone, as I have been by far pleased with some of the top helpers on this forum, I have decided to send another one your way.

Even though I am personally having minimal issues with this script I have developed, I am wondering if someone could fix my little bug with it.

This script runs perfectly fine, except once in a while, the color changing will "pause" because (according to Mozilla Firefox 3's error console) it is putting out invalid color codes. It only does a short burst of 8 invalid color codes (on average) and then gets back in a normal range.

I feel I'm missing something, maybe a fresh pair of eyes can spot it out.

Live Demo Here

Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2.  
  3. var shinylink = (false);
  4. var custom_ids = (0);
  5.  
  6. var maximum = (64); // This is the maximum any color value can reach.
  7. var minimum = (0); // This is the minimum any color value can reach.
  8.  
  9. var objects = (0);
  10. var active_object = (false);
  11. var active_color = (false);
  12.  
  13. var using_objects = new Array ();
  14. var restore_objects = new Array ();
  15. var object_references = new Array ();
  16.  
  17. function d2h(d)
  18.     {
  19.         var h = d.toString(16);
  20.         if(h.length == (1))
  21.             {
  22.                 h = "0"+h;
  23.             }
  24.         return (h);
  25.     }
  26. function h2d(h)
  27.     {
  28.         var d = parseInt(h, (16));
  29.         return (d);
  30.     }
  31. function split_color (color)
  32.     {
  33.         var acceptable = '0123456789abcdefABCDEF';
  34.         var string = '';
  35.  
  36.  
  37.         for (var i = (0); i < color.length; i++)
  38.             {
  39.                 if (acceptable.indexOf (color.charAt (i)) > (-1))
  40.                     {
  41.                         string += color.charAt (i);
  42.                     }
  43.             }
  44.  
  45.         var red = string.charAt (0) + string.charAt (1);
  46.         var green = string.charAt (2) + string.charAt (3);
  47.         var blue = string.charAt (4) + string.charAt (5);
  48.  
  49.         return (Array (h2d (red), h2d (green), h2d (blue)));
  50.     }
  51.  
  52. function shinylink_initiate (object, color)
  53.     {
  54.         if (window.shinylink == (false))
  55.             {
  56.                 var links = document.getElementsByTagName ('a');
  57.                 for (var i = (0); i < links.length; i++)
  58.                     {
  59.                         if (links[window.custom_ids++].id == '')
  60.                             {
  61.                                 links[i].id = 'shinylink_' + window.custom_ids;
  62.                             }
  63.                     }
  64.             }
  65.         if (object.id == '')
  66.             {
  67.                 object.id = 'shinylink_' + window.custom_ids++;
  68.             }
  69.         var number = window.objects++;
  70.         var color_array = split_color (color);
  71.  
  72.         if (typeof (window.using_objects[number]) == 'undefined')
  73.             {
  74.                 window.using_objects[number] = (false);
  75.                 window.object_references[number] = object.id;
  76.             }
  77.  
  78.         object.onmouseover = function ()
  79.             {
  80.                 shinylink_mouseover (number, color_array);
  81.             }
  82.         object.onmouseout = function ()
  83.             {
  84.                 shinylink_mouseout (number, color_array);
  85.             }
  86.  
  87.         window.shinylink = (true);
  88.  
  89.         shinylink_mouseover (number, color_array);
  90.     }
  91. function shinylink_mouseover (id, color)
  92.     {
  93.         window.active_object = id;
  94.         window.active_color = (false);
  95.  
  96.         shinylink_scan (id, color);
  97.     }
  98. function shinylink_mouseout (id, color)
  99.     {
  100.         if (window.active_object == id)
  101.             {
  102.                 window.active_object = (false);
  103.                 window.active_color = (false);
  104.             }
  105.  
  106.         shinylink_restore (id, color);
  107.     }
  108. function shinylink_scan (id, color)
  109.     {
  110.         var input_color = Array (
  111.             Math.round ((color[0] + (1)) / (4)),
  112.             Math.round ((color[1] + (1)) / (4)),
  113.             Math.round ((color[2] + (1)) / (4))
  114.         );
  115.  
  116.         if (window.active_object === id)
  117.             {
  118.                 var current = window.using_objects[id] != (false) ? window.using_objects[id] : input_color;
  119.  
  120.                 if ((current[0] == window.active_color[0] && current[1] == window.active_color[1] && current[2] == window.active_color[2]) || window.active_color == (false))
  121.                     {
  122.                         if (window.active_color == (false))
  123.                             {
  124.                                 window.active_color = new Array ();
  125.                             }
  126.  
  127.                         // Pick a random color.
  128.  
  129.                         // Pick which colors will change.
  130.                         var change_red = Math.round (Math.random ());
  131.                         var change_green = Math.round (Math.random ());
  132.                         var change_blue = Math.round (Math.random ());
  133.  
  134.                         if (change_red == (0) && change_green == (0) && change_blue == (0))
  135.                             {
  136.                                 var change_rgb = Math.round (Math.random () * (2));
  137.  
  138.                                 change_red = change_rgb == (0) ? (1) : (0);
  139.                                 change_green = change_rgb == (1) ? (1) : (0);
  140.                                 change_blue = change_rgb == (2) ? (1) : (0);
  141.                             }
  142.  
  143.                         if (change_red == (1))
  144.                             {
  145.                                 var direction_red = current[0] <= window.minimum ? (1) : current[0] >= window.maximum ? (0) : Math.round (Math.random ());
  146.  
  147.                                 var maximum_distance_red = direction_red == (0) ? current[0] : (window.maximum - current[0]);
  148.  
  149.                                 // Increase chances of picking the highest number possible.
  150.                                 // Decrease chances of picking the smallest number possible.
  151.                                 var random_distance_chance_red = (maximum_distance_red * maximum_distance_red);
  152.                                 var random_distance_pick_red = Math.round (Math.random () * random_distance_chance_red);
  153.  
  154.                                 var i_red = (1);
  155.                                 while (random_distance_pick_red >= (i_red * i_red))
  156.                                     {
  157.                                         i_red++;
  158.                                     }
  159.                                 var distance_red = (i_red - (1));
  160.  
  161.                                 window.active_color[0] = direction_red == (0) ? (current[0] - distance_red) : (current[0] + distance_red);
  162.                             }
  163.                         else
  164.                             {
  165.                                 window.active_color[0] = current[0];
  166.                             }
  167.  
  168.                         if (change_green == (1))
  169.                             {
  170.                                 var direction_green = current[1] <= window.minimum ? (1) : current[1] >= window.maximum ? (0) : Math.round (Math.random ());
  171.  
  172.                                 var maximum_distance_green = direction_green == (0) ? current[1] : (window.maximum - current[1]);
  173.  
  174.                                 // Increase chances of picking the highest number possible.
  175.                                 // Decrease chances of picking the smallest number possible.
  176.                                 var random_distance_chance_green = (maximum_distance_green * maximum_distance_green);
  177.                                 var random_distance_pick_green = Math.round (Math.random () * random_distance_chance_green);
  178.  
  179.                                 var i_green = (1);
  180.                                 while (random_distance_pick_green >= (i_green * i_green))
  181.                                     {
  182.                                         i_green++;
  183.                                     }
  184.                                 var distance_green = (i_green - (1));
  185.  
  186.                                 window.active_color[1] = direction_green == (0) ? (current[1] - distance_green) : (current[1] + distance_green);
  187.                             }
  188.                         else
  189.                             {
  190.                                 window.active_color[1] = current[1];
  191.                             }
  192.  
  193.                         if (change_blue == (1))
  194.                             {
  195.                                 var direction_blue = current[2] <= window.minimum ? (1) : current[2] >= window.maximum ? (0) : Math.round (Math.random ());
  196.  
  197.                                 var maximum_distance_blue = direction_blue == (0) ? current[2] : (window.maximum - current[2]);
  198.  
  199.                                 // Increase chances of picking the highest number possible.
  200.                                 // Decrease chances of picking the smallest number possible.
  201.                                 var random_distance_chance_blue = (maximum_distance_blue * maximum_distance_blue);
  202.                                 var random_distance_pick_blue = Math.round (Math.random () * random_distance_chance_blue);
  203.  
  204.                                 var i_blue = (1);
  205.                                 while (random_distance_pick_blue >= (i_blue * i_blue))
  206.                                     {
  207.                                         i_blue++;
  208.                                     }
  209.                                 var distance_blue = (i_blue - (1));
  210.  
  211.                                 window.active_color[2] = direction_blue == (0) ? (current[2] - distance_blue) : (current[2] + distance_blue);
  212.                             }
  213.                         else
  214.                             {
  215.                                 window.active_color[2] = current[2];
  216.                             }
  217.                     }
  218.  
  219.                 // Transition one color maximum per channel.
  220.                 var new_color = new Array ();
  221.                 var color_string = '#';
  222.                 for (var i = (0); i < window.active_color.length; i++)
  223.                     {
  224.                         new_color[i] = current[i] < window.active_color[i] ? (current[i] + (1)) : current[i] > window.active_color[i] ? (current[i] - (1)) : current[i];
  225.                         color_string += d2h (new_color[i] != (0) ? ((new_color[i] * (4)) - (1)) : (0));
  226.                     }
  227.  
  228.                 window.using_objects[id] = new_color;
  229.                 window.restore_objects[id] = Array (
  230.                     ((new_color[0] * (4)) - (1)),
  231.                     ((new_color[1] * (4)) - (1)),
  232.                     ((new_color[2] * (4)) - (1))
  233.                 );
  234.  
  235.                 document.getElementById(window.object_references[id]).style.color = color_string;
  236.  
  237.                 setTimeout ('shinylink_scan (' + id + ', ' + color + '); ', (5));
  238.             }
  239.     }
  240. function shinylink_restore (id, color)
  241.     {
  242.         var current = window.using_objects[id] != (false) ? window.restore_objects[id] : color;
  243.         if ((current[0] != color[0] || current[1] != color[1] || current[2] != color[2]) && window.active_object !== id)
  244.             {
  245.                 // Transition one color maximum per channel.
  246.                 var new_color = new Array ();
  247.                 var color_string = '#';
  248.                 for (var i = (0); i < color.length; i++)
  249.                     {
  250.                         new_color[i] = current[i] < color[i] ? (current[i] + (1)) : current[i] > color[i] ? (current[i] - (1)) : current[i];
  251.                         color_string += d2h (new_color[i]);
  252.                     }
  253.                 window.restore_objects[id] = new_color;
  254.                 window.using_objects[id] = Array (
  255.                     ((new_color[0] + (1)) / (4)),
  256.                     ((new_color[1] + (1)) / (4)),
  257.                     ((new_color[2] + (1)) / (4))
  258.                 );
  259.  
  260.                 document.getElementById(window.object_references[id]).style.color = color_string;
  261.  
  262.                 setTimeout ('shinylink_restore (' + id + ', Array (' + color[0] + ', ' + color[1] + ', ' + color[2] + ')); ', (5));
  263.             }
  264.     }
Aug 5 '08 #1
11 1830
moltendorf
65 New Member
I just realized I had an old thread going for this same script, but that was with a completely different beginner issue (I was still learning how to assign an object in the page an event function on the fly, which I accomplished in this), this is much more advanced, and complete version of the code, which is having one little issue that I think is related to some addition or multiplication that I forgot to apply to the numbers somewhere in the code. I thought I figured it out in my head last night while I was away from the computer, (and yet again) came back to it only to realize I didn't know what I was thinking about.

This is the original:
http://bytes.com/forum/thread748828.html
Aug 5 '08 #2
acoder
16,027 Recognized Expert Moderator MVP
I don't know if you've modified it since, but I don't see any errors.
Aug 5 '08 #3
moltendorf
65 New Member
There is no "error" I guess I should say, but the problem is that it comes up with an incorrect color code. I feel my math is off somewhere... It works, but just mouseover the link on the page, keep the JavaScript error console up (in Mozilla or Opera seem to have good ones), and just wait, eventually you will see a burst of invalid color codes produced by this script.

I guess they are more of warnings then errors, but I'm trying to rid my script of all bugs.

This is the slightly modified code I made after I finished the page demo I provided to you. I only really fixed the spelling errors in the names of the functions, and then stuck it in a .js file so I could easily use it on my forums.

Anyone willing to put a bit of effort into checking this out has my gratitude. :)

Oh, and acoder, thank you for your help in some of my previous questions.
Aug 5 '08 #4
acoder
16,027 Recognized Expert Moderator MVP
Can you post the warning/error messages that you see and which line no. it points to.
Aug 6 '08 #5
moltendorf
65 New Member
document.getEle mentById(window .object_referen ces[id]).style.color = color_string;

On line 235.

It has nothing to do with that line having the actual error, the error is within the shinylink_scan function.

Because it is setting color_string to an invalid color code which again, is probably because I messed up on my addition somehow.

I'm just wondering if anyone here wants to take the time to sit down and really look at the function to point out where I may have a calculation that can cause the number that is converted into hex code to be over 255, or under 0.
Aug 8 '08 #6
moltendorf
65 New Member
I could put in a simple check system that reduces it to that maximum or minimum value, but I'd prefer the numbers come out correct in the first place.
Aug 8 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Have you tried using a debugger? A useful one is Firebug for Firefox.
Aug 8 '08 #8
FLEB
30 New Member
Not a solution, but a possible strategy: Instead of mucking around with hex conversions, it might just be simpler to have it give the object its color in "rgb(r,g,b) " format. It's (IIRC) cross-browser friendly, and the r,g, and b values are just decimals from 0 to 255.
Aug 8 '08 #9
moltendorf
65 New Member
Thanks for the tip, acoder, and FLEB. I didn't think about using the rgb CSS value initially since I have been so used to using #RRGGBB format.I will give it a try, see if they help me get the numbers I want.
Aug 9 '08 #10

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

Similar topics

6
1877
by: The Bicycling Guitarist | last post by:
I've used and loved the webmaster tools at Delorie.com for some years now. I just added some more text content to my home page, but in the web page "purifier" the text doesn't render as I thought it should. It looks fine in MSIE 6, lol. I didn't want the text to be centered, and specified a css class to clear the floats above and text-align to the left. Does it look that way for other people using other browsers than MSIE? If not, is it a...
68
3990
by: Ted Nicols | last post by:
That's what I keep asking myself whenever develop in .NET. Is this a joke a farse or just a bad dream? ..NET is slow, actually slow is just a polite word I can use in a newsgroup. ..NET is just a VM and as one it works very slowly, before and after JIT. I don't understand why some people insist that a VM can be compared with native code. I really feel sick, whenever compare native C++ code against
3
1147
by: Guido Wesdorp | last post by:
======================== Kupu 1.1 beta 1 released ======================== 22 May 2004 The Kupu Team is pround to announce the first beta release of Kupu 1.1. After the 1.0.3 release, which was the first one to carry the new name and license, the new version brings new, long-awaited features.
335
11653
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
37
3926
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
3
3031
by: dianedebuda | last post by:
Using only Access 97. Need to run as admin in order to use Tool /Compact, etc. Tried Allen Browne's suggestion about registry hack meant for multiple versions of Access, but doesn't seem to work for "version 8.". (Still get Error 75: Path/File access error) Users have been using desktop icons pointing to specific .mdb. Although "Run as Administrator" is available for short-cut to msaccess.exe, it is grayed out when specific mdb is added...
6
4272
by: John | last post by:
Hi If I have a choice of WPF or Windows Form versions of a control which one should I purchase? Thanks Regards
3
11195
by: Jethro | last post by:
Hi guys, I have inherited a .NET 2.0 web project. It consists of a root site, and 3 sub directories. Ideally these are seperate projects/websites. The root is a customer- facing one, and the other 3 are for admin, and supervisor use. I want to break the existing project (all websites treated as one) into 4 separate websites.
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.