473,509 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AddEvent: "Invalid Label" Error in Code that Works for Others

RMWChaos
137 New Member
Darnit all, I expect the code I steal from others to work! =D

Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the blog where it was posted, it seems that it works for everyone who posted replies...not a single "this is not working for me" post.

The errors I receive are as follows:

IE7: expected ";" and then "conditional compilation is turned off"

FF & NN: "invalid label"

When I run debug in IE, Firebug, and NN Error Console, all three point the same line of code:

Expand|Select|Wrap|Line Numbers
  1. add : function(node, sEventName, fHandler)
I've done some research around the "invalid label" issue, but all point to the same problem in JSON where an additional set of parenthesis is required, specifically in the syntax:

Expand|Select|Wrap|Line Numbers
  1. ( "(" + someCode + ")" )
But I'll be darned if I can figure out where to include them--I've tried everywhere I can think of. I posted this same question on the blog where I found the supposedly Rock Solid addEvent() code (link included here for reference only, not a plug =D ), but the last post was around August; so no indication that there will be a reply anytime soon or at all.

Any thoughts on what the problem is, or if you believe you have a better code for initiating external javascript files on page load, please let me know!

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. function addEvent(obj, type, fn)
  2.  
  3.     {
  4.  
  5.     if (obj.addEventListener)
  6.  
  7.         {
  8.  
  9.         // standard
  10.  
  11.         obj.addEventListener(type, fn, false);
  12.  
  13.         // bugzilla bug #241518
  14.  
  15.         EventCache.add(obj, type, fn);
  16.  
  17.         }
  18.  
  19.     else if (obj.attachEvent)
  20.  
  21.         {
  22.  
  23.         var func = function()
  24.  
  25.             {
  26.  
  27.             fn.apply(window.event.srcElement);
  28.  
  29.             };
  30.  
  31.         obj.attachEvent("on" + type, func);
  32.  
  33.         EventCache.add(obj, type, func);
  34.  
  35.         }
  36.  
  37.     else if (typeof obj['on'+type] != 'function')
  38.  
  39.         {
  40.  
  41.         obj['on'+type] = fn;
  42.  
  43.         }
  44.  
  45.     else
  46.  
  47.         {
  48.  
  49.         // really old
  50.  
  51.         var oldonload = obj['on'+type];
  52.  
  53.         obj['on'+type] = function()
  54.  
  55.             {
  56.  
  57.             oldonload();
  58.  
  59.             fn();
  60.  
  61.             }
  62.  
  63.         }
  64.  
  65.     }
  66.  
  67.  
  68.  
  69. function removeEvent(obj, type, fn)
  70.  
  71.     {
  72.  
  73.     EventCache.remove(obj, type, fn);
  74.  
  75.     }
  76.  
  77.  
  78.  
  79. var EventCache = function()
  80.  
  81.     {
  82.  
  83.     var listEvents = [];
  84.  
  85.     return
  86.  
  87.         {
  88.  
  89.         listEvents : listEvents,
  90.  
  91.         add : function(node, sEventName, fHandler)
  92.  
  93.             {
  94.  
  95.             listEvents.push(arguments);
  96.  
  97.             },
  98.  
  99.         remove : function(node, sEventName, fHandler)
  100.  
  101.             {
  102.  
  103.             var i, item;
  104.  
  105.             for(i = listEvents.length - 1; i >= 0; i = i - 1)
  106.  
  107.                 {
  108.  
  109.                 if(node == listEvents[i][0] && sEventName == listEvents[i][1] && fHandler == listEvents[i][2])
  110.  
  111.                     {
  112.  
  113.                     item = listEvents[i];
  114.  
  115.                     if(item[0].removeEventListener)
  116.  
  117.                         {
  118.  
  119.                         item[0].removeEventListener(item[1], item[2], item[3]);
  120.  
  121.                         }
  122.  
  123.                     if(item[1].substring(0, 2) != “on”)
  124.  
  125.                         {
  126.  
  127.                         item[1] = “on” + item[1];
  128.  
  129.                         }
  130.  
  131.                     if(item[0].detachEvent)
  132.  
  133.                         {
  134.  
  135.                         item[0].detachEvent(item[1], item[0][sEventName+fHandler]);
  136.  
  137.                         }
  138.  
  139.                     item[0][item[1]] = null;
  140.  
  141.                     }
  142.  
  143.                 }
  144.  
  145.             },
  146.  
  147.         flush : function()
  148.  
  149.             {
  150.  
  151.             var i, item, eventtype;
  152.  
  153.             for(i = listEvents.length - 1; i >= 0; i = i - 1)
  154.  
  155.                 {
  156.  
  157.                 item = listEvents[i];
  158.  
  159.                 if(item[0].removeEventListener)
  160.  
  161.                     {
  162.  
  163.                     item[0].removeEventListener(item[1], item[2], item[3]);
  164.  
  165.                     }
  166.  
  167.                 eventtype = item[1];
  168.  
  169.                 if(item[1].substring(0, 2) != “on”)
  170.  
  171.                     {
  172.  
  173.                     item[1] = ‘on’ + item[1];
  174.  
  175.                     }
  176.  
  177.                 if(item[0].detachEvent)
  178.  
  179.                     {
  180.  
  181.                     item[0].detachEvent(item[1], item[2]);
  182.  
  183.                     item[0].detachEvent(item[1], item[0][eventtype+item[2]]);
  184.  
  185.                     }
  186.  
  187.                 item[0][item[1]] = null;
  188.  
  189.                 }
  190.  
  191.             }
  192.  
  193.         }
  194.  
  195.     }();
  196.  
  197. /* **** End: Global Functions & Vars **** */
  198.  
  199. /* **** Begin: Add Events **** */
  200.  
  201. addEvent(window, "unload", EventCache.flush);
  202.  
Oct 22 '07 #1
4 3313
RMWChaos
137 New Member
Man, I have spent all day on this bugger, and through sheer brute force, I figured out the problem; something I never would have guessed otherwise.

These lines of code in var EventCache:
Expand|Select|Wrap|Line Numbers
  1. (...)
  2. return
  3.      {
  4. (...)
  5.  
should be written like this:
Expand|Select|Wrap|Line Numbers
  1. (...)
  2. return {
  3. (...)
  4.  
Now did I just miss that somewhere in all my reference materials? Is that standard javascript syntax, or is it specific to JSON?

As for the "conditional compilation is turned off" error in MSIE, that's just a matter of putting /*@cc_on */ and /*@end */ in the right places. Not that I have a clue how that whole mess works. Sigh...more reading to do.

Like I said, it was an ID-10-T error all along. =\
Oct 22 '07 #2
gits
5,390 Recognized Expert Moderator Expert
hi ...

i'm not quite sure ... but i think that is a specific problem with the return ... since the parser can parse everything you wrote without syntax-errors. so the parser had no chance to find out that you didn't want to have the object a new statement but wanted it as the return-value. this ist due to the (or only some?) parser behaviour(s) that allow to write statements that don't end with a semicolon ; ... its silly but its how it is ;)

best way to avoid this is to always use the following syntax:

Expand|Select|Wrap|Line Numbers
  1. var obj = {};
  2.  
  3. return obj;
  4.  
looks much clearer doesn't it? i know, of course somebody will not think so ... but it do :)

kind regards
Oct 23 '07 #3
RMWChaos
137 New Member
It's gits to my rescue again! =D

So you are saying the best way to approach this is to assign the full code in the current return statement to a var, then return the var. Hm, that makes sense to me, and yes, much clearer. So, the code would look something like this:

Expand|Select|Wrap|Line Numbers
  1. var eventReturn = function() 
  2.         {
  3.  
  4.         listEvents : listEvents,
  5.  
  6.         add : function(node, sEventName, fHandler)
  7.  
  8.        (...)
  9.  
  10.         };
  11.  
  12. var EventCache = function()
  13.  
  14.     {
  15.  
  16.     var listEvents = [];
  17.  
  18.     return eventReturn;
  19.  
  20.     };
  21.  
Oct 25 '07 #4
gits
5,390 Recognized Expert Moderator Expert
hi ...

yes ... exactly :) i think you know that using:

Expand|Select|Wrap|Line Numbers
  1. var o = {};
  2.  
literally creates an object instance. and it is equivalent to use

Expand|Select|Wrap|Line Numbers
  1. var o = new Object;
so when using:

Expand|Select|Wrap|Line Numbers
  1. return {};
  2.  
you would first return an anonymous object that you assing to a variable i guess. since objects are passed by reference you could use var o instead and pass the reference of o through the return to a new variable ...

kind regards
Oct 25 '07 #5

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

Similar topics

0
4588
by: Brian Morris | last post by:
I'm new to .NET and just trying a few things out, like emailing. I created a form in Visual Studio .Net to input some information for generating an email and I'm getting the following error when it...
9
1966
by: Wally | last post by:
I am trying to display images from an Access 2000 database and I get an error "Invalid Parameter Used" when I execute the code line "picBLOB.Image = Image.FromStream(stmBLOBData)" in my Visual...
1
3958
by: verb13 | last post by:
In an asp page in localhost I have this: Set x = Server.CreateObject("Scripting.Dictionary") And I get this error: Invalid class string If I omit Server like this: Set x =...
1
5392
by: John Hunter | last post by:
I've recently had a nasty problem with the "Invalid reference to the property Form" error in subforms - nasty because it doesn't seem to consistently happen to all forms which contain the same...
3
29204
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
7
17858
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
0
2140
by: Winterminute | last post by:
I am trying to read a list of install programs using WMI with ASP.NET/C#. However, it fails with "Invalid Class". I have confirmed that if I query LOCALHOST then it works fine, but if I query a...
5
14302
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
11
2483
by: markryde | last post by:
Hello, Followed here is a simplified code example of something which I try to implement; in essence , I want to assign a value to a return value of a method is C. I know, of course, that in this...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7416
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...
1
7073
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...
0
7506
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...
0
5656
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,...
0
1571
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 ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
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...

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.