Hi,
I had a problem with prototypejs when it came to for ... in loops for
associative arrays. Usually this gets solved by changing the array
into object (or changing the loop for regular arrays), but in some
cases I had to leave the possibility for a function to accept
associative arrays anyhow. This is how I solved it.
Within the loop you can check if the array value is an actual value or
a prototype method, by checking the constructor property of it. This
seems to work fine, except in Safari (on Mac) and Konqueror (on Linux)
(just tested it briefly on 3 platforms with major browsers). So with
Safari and Konqueror I just check the value itself for the occurrence
of 'Function(', 'Native code', ... This check is done by the
'isValue()' function (see below), returning false if the argument is a
method, true otherwise. Sniffing the browser is done with a set of
booleans I usually use (see all below) .
I know this is not too good practice, but I just needed a quick
solution. Hope it comes in handy for some of you as well.
So there you go ...
-------------------------------------------------------------------------------------------
for(x in myArray) {
if(isValue(myArray[x])) {
// do something ...
}
}
function isValue(val) {
if(navIsSafari || navIsKonqueror) {
checkStr = val.toString();
if(checkStr.indexOf('function(') != -1 ||
checkStr.indexOf('Function(') != -1 || checkStr.indexOf('function (') !
= -1 || checkStr.indexOf('Function (') != -1 ||
checkStr.indexOf('internal function') != -1 ||
checkStr.indexOf('Internal Function') != -1 ||
checkStr.indexOf('native code') != -1 || checkStr.indexOf('Native
Code') != -1) return false;
}
else {
checkStr = val.constructor.toString();
if(checkStr.indexOf('function(') != -1 ||
checkStr.indexOf('Function(') != -1 || checkStr.indexOf('function (') !
= -1 || checkStr.indexOf('Function (') != -1 ||
checkStr.indexOf('internal function') != -1 ||
checkStr.indexOf('Internal Function') != -1) return false;
}
return true;
}
-------------------------------------------------------------------------------------------
var navIsOpera = (navigator.userAgent.indexOf('Opera') != -1);
var navIsNetscape = (navigator.userAgent.indexOf('Netscape') != -1);
var navIsSafari = (navigator.userAgent.indexOf('Safari') != -1);
var navIsFirefox = (navigator.userAgent.indexOf('Firefox') != -1);
var navIsKonqueror = (navigator.userAgent.indexOf('Konqueror') != -1);
var navIsIexplorer = (navigator.userAgent.indexOf('MSIE') != -1 && !
navIsOpera && !navIsNetscape);
var navIsMozilla = (navigator.userAgent.indexOf('Mozilla') != -1 && !
navIsOpera && !navIsNetscape && !navIsSafari && !navIsIexplorer && !
navIsFirefox && !navIsKonqueror);
var navIsOther = (!navIsOpera && !navIsNetscape && !navIsSafari && !
navIsFirefox && !navIsKonqueror && !navIsIexplorer && !navIsMozilla);
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---