HighDots Forums  

message of 'is null or not an object'

Javascript JavaScript language (comp.lang.javascript)


Discuss message of 'is null or not an object' in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
SAM
 
Posts: n/a

Default Re: message of 'is null or not an object' - 06-13-2008 , 06:02 AM






RobG a écrit :
Quote:
On Jun 13, 8:59 am, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid
wrote:
[...]
script type='text/javascript'

function $(id) // multi browser getter of an element

There has recently been discussion about using $ as a function name,
it is not considered a good idea around here:

"Replacing document.getElementById with $ (dollar sign)"
URL
http://groups.google.com.au/group/co...8f81 84cd5a4e
That overall seems to discus about saving few octets and the almost
reserved sign $ (and it's use in most libraries)
Here the idea is to "translate" GBEI and I think it's a good progress
relatively to the initial code from OP.

Quote:
{
return typeof id != 'string'? id :
document.getElementById?
document.getElementById(id) :
document.all?
document.all[id] :
document.forms[0].elements[id]; // suppose an alone form
}

A more efficient way is to test the supported features and create an
appropriate function up front, then you don't have to test for
getElementById and all every time, e.g.
You mean with this code the call to getElement('anId')
will no more run the function (its if then or) ?
The translation is made once for all the page.

Quote:
var getElement = (function() {
if (document) {
if (document.getElementById) {
return function(id) {
return (typeof id == 'string')? document.getElementById(id) :
id;
}
} else if (document.all) {
return function(id) {
return (typeof id == 'string')? document.all(id) : id;
}
}
}
})();

Quote:
function ConfirmDeleteImage() // would have to work with any browser
{
var cbo = $('cboUnitTypes');
if (cbo.options.length <= 0)

I can't imagine a scenario where the length of an options collection
I think it was a test to see if the JS did fill up the options
(not html coded)

Quote:
can be less than zero. Anyway, the length of an options collection is
an unsigned long so it can't be negative - providing the UA is
standards comliant of course. :-)
Not a real problem the goal is the 0 value

Quote:
{
alert('I do not understand why the function did fire');
return false;
}
if (cbo.selectedIndex == 0)

This test should be less than or equal to zero since the selectedIndex
should be -1 if no option has been selected.
without selection no onchange, no ?
so that can never be -1 if a change was made

Quote:
{
alert('Choice another item in the list');

Choose? :-)
may be, may be :-)

--
sm


Reply With Quote
  #12  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: message of 'is null or not an object' - 06-13-2008 , 02:12 PM






RobG wrote:
Quote:
SAM wrote:
[...]
script type='text/javascript'

function $(id) // multi browser getter of an element
[...]
{
return typeof id != 'string'? id :
document.getElementById?
document.getElementById(id) :
document.all?
document.all[id] :
document.forms[0].elements[id]; // suppose an alone form
}

A more efficient way is to test the supported features and create an
appropriate function up front, then you don't have to test for
getElementById and all every time, e.g.

var getElement = (function() {
if (document) {
if (document.getElementById) {
return function(id) {
return (typeof id == 'string')? document.getElementById(id) :
^^^^^^^^^^^^^^^^^^^^^^^^
id;
}
} else if (document.all) {
return function(id) {
return (typeof id == 'string')? document.all(id) : id;
^^^^^^^^^^^^^^^^^^^^^^^^
}
}
}
})();
One would buy efficiency with runtime flexibility, of course.

However, I do not see why a user-defined method should waste runtime for
supporting the incompetence of the person calling it. IMHO this function
should expect a string value (or something convertible to that, but this
should be left to the DOM API) and nothing else.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7 (AT) news (DOT) demon.co.uk>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.