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: - 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:
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: -
function addEvent(obj, type, fn)
-
-
{
-
-
if (obj.addEventListener)
-
-
{
-
-
// standard
-
-
obj.addEventListener(type, fn, false);
-
-
// bugzilla bug #241518
-
-
EventCache.add(obj, type, fn);
-
-
}
-
-
else if (obj.attachEvent)
-
-
{
-
-
var func = function()
-
-
{
-
-
fn.apply(window.event.srcElement);
-
-
};
-
-
obj.attachEvent("on" + type, func);
-
-
EventCache.add(obj, type, func);
-
-
}
-
-
else if (typeof obj['on'+type] != 'function')
-
-
{
-
-
obj['on'+type] = fn;
-
-
}
-
-
else
-
-
{
-
-
// really old
-
-
var oldonload = obj['on'+type];
-
-
obj['on'+type] = function()
-
-
{
-
-
oldonload();
-
-
fn();
-
-
}
-
-
}
-
-
}
-
-
-
-
function removeEvent(obj, type, fn)
-
-
{
-
-
EventCache.remove(obj, type, fn);
-
-
}
-
-
-
-
var EventCache = function()
-
-
{
-
-
var listEvents = [];
-
-
return
-
-
{
-
-
listEvents : listEvents,
-
-
add : function(node, sEventName, fHandler)
-
-
{
-
-
listEvents.push(arguments);
-
-
},
-
-
remove : function(node, sEventName, fHandler)
-
-
{
-
-
var i, item;
-
-
for(i = listEvents.length - 1; i >= 0; i = i - 1)
-
-
{
-
-
if(node == listEvents[i][0] && sEventName == listEvents[i][1] && fHandler == listEvents[i][2])
-
-
{
-
-
item = listEvents[i];
-
-
if(item[0].removeEventListener)
-
-
{
-
-
item[0].removeEventListener(item[1], item[2], item[3]);
-
-
}
-
-
if(item[1].substring(0, 2) != “on”)
-
-
{
-
-
item[1] = “on” + item[1];
-
-
}
-
-
if(item[0].detachEvent)
-
-
{
-
-
item[0].detachEvent(item[1], item[0][sEventName+fHandler]);
-
-
}
-
-
item[0][item[1]] = null;
-
-
}
-
-
}
-
-
},
-
-
flush : function()
-
-
{
-
-
var i, item, eventtype;
-
-
for(i = listEvents.length - 1; i >= 0; i = i - 1)
-
-
{
-
-
item = listEvents[i];
-
-
if(item[0].removeEventListener)
-
-
{
-
-
item[0].removeEventListener(item[1], item[2], item[3]);
-
-
}
-
-
eventtype = item[1];
-
-
if(item[1].substring(0, 2) != “on”)
-
-
{
-
-
item[1] = ‘on’ + item[1];
-
-
}
-
-
if(item[0].detachEvent)
-
-
{
-
-
item[0].detachEvent(item[1], item[2]);
-
-
item[0].detachEvent(item[1], item[0][eventtype+item[2]]);
-
-
}
-
-
item[0][item[1]] = null;
-
-
}
-
-
}
-
-
}
-
-
}();
-
-
/* **** End: Global Functions & Vars **** */
-
-
/* **** Begin: Add Events **** */
-
-
addEvent(window, "unload", EventCache.flush);
-
4 3313
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:
should be written like this:
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. =\
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: -
var obj = {};
-
-
return obj;
-
looks much clearer doesn't it? i know, of course somebody will not think so ... but it do :)
kind regards
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: -
var eventReturn = function()
-
{
-
-
listEvents : listEvents,
-
-
add : function(node, sEventName, fHandler)
-
-
(...)
-
-
};
-
-
var EventCache = function()
-
-
{
-
-
var listEvents = [];
-
-
return eventReturn;
-
-
};
-
gits 5,390
Recognized Expert Moderator Expert
hi ...
yes ... exactly :) i think you know that using:
literally creates an object instance. and it is equivalent to use
so when using:
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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 =...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |