shortcut for document.getElementById and some error

H

HopfZ

I made two shortcut functions for document.getElementById as:

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;

But EBI3 don't work.

EBI2('myText'); //works
EBI3('myText'); //unexpected error

The error message for EBI3 according to Firebug Firefox(1.5.0.4)
extension is:
uncaught exception: [Exception... "Illegal operation on WrappedNative
prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame :: javascript:
with (FireBugEval.api) { with (window) {
FireBugEval(eval(FireBugEval.expr)) }} :: <TOP_LEVEL> :: line 1" data:
no]

Is EBI3 a bad way?
 
M

Martin Honnen

HopfZ said:
I made two shortcut functions for document.getElementById as:

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;

But EBI3 don't work.

EBI2('myText'); //works
EBI3('myText'); //unexpected error

You could do e.g.
document.EBI3 = document.getElementById
and I think Firefox will then not complain on
document.EBI3('myText')
calls.
But if you try to use the method without calling it on the proper
object, the document object, you are in trouble as you have found.
 
R

Randy Webb

HopfZ said the following on 6/30/2006 11:57 AM:
I made two shortcut functions for document.getElementById as:

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;

But EBI3 don't work.

EBI2('myText'); //works

Because EBI2 is a function that returns an object if it exists.
EBI3('myText'); //unexpected error

EB13 is just a reference to a native object, its not a function, just a
variable is all.
The error message for EBI3 according to Firebug Firefox(1.5.0.4)
extension is:
uncaught exception: [Exception... "Illegal operation on WrappedNative
prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame :: javascript:
with (FireBugEval.api) { with (window) {
FireBugEval(eval(FireBugEval.expr)) }} :: <TOP_LEVEL> :: line 1" data:
no]

Is EBI3 a bad way?

Yes.

gEBI might be a better name for them though.
 
E

Evertjan.

HopfZ wrote on 30 jun 2006 in comp.lang.javascript:
function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;

But EBI3 don't work.

EBI2('myText'); //works
EBI3('myText'); //unexpected error

Think & try:

r = EBI3['myText']

or:

r = EBI3.myText
 
H

HopfZ

Evertjan. said:
HopfZ wrote on 30 jun 2006 in comp.lang.javascript:
function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;

But EBI3 don't work.

EBI2('myText'); //works
EBI3('myText'); //unexpected error

Think & try:

r = EBI3['myText']

or:

r = EBI3.myText

Both returns null (without error).

--------- test.html ----------------
<html>
<head>
<title>test</title>
<script language="javascript">
function load(){
function assert(b,s){ if(!b) alert('false is\n'+s);}

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;
document.EBI4 = document.getElementById;

assert(EBI2('myText'),'EBI2 works');
assert(EBI3['myText']==null,'EBI3["myText"] returns null');
assert(EBI3.myText==null,'EBI3.myText returns null');
assert(document.EBI4.myText==null, 'document.EBI4 works');

alert('end of load');
}
</script>
</head>
<body onload="load();">
<form id="myForm">
<input type="text" id="myText"></input>
</form>
</body>
</html>
 
E

Evertjan.

HopfZ wrote on 30 jun 2006 in comp.lang.javascript:
Evertjan. said:
r = EBI3['myText']

or:

r = EBI3.myText

Both returns null (without error).

You are right, my mistake.

getElementById needs an (argument)
--------- test.html ----------------
<html>
<head>
<title>test</title>
<script language="javascript">

use said:
function load(){
function assert(b,s){ if(!b) alert('false is\n'+s);}

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;
document.EBI4 = document.getElementById;

assert(EBI2('myText'),'EBI2 works');
assert(EBI3['myText']==null,'EBI3["myText"] returns null');

Do not test for null, test for existence. see below.
assert(EBI3.myText==null,'EBI3.myText returns null');
assert(document.EBI4.myText==null, 'document.EBI4 works');

document.EBI4.myText does not work too !!!!!
alert('end of load');
}
</script>
</head>
<body onload="load();">
<form id="myForm">
<input type="text" id="myText"></input>

</form>
</body>
</html>

Try this:

<html>
<head>
<script type='text/javascript'>
function load(){
function assert(b,s){ if(!b) alert(s);}

function EBI2(id){return document.getElementById(id)};
var EBI3 = document.getElementById;
document.EBI4 = document.getElementById;

assert(EBI2('myText'),'EBI2 error'); // OK SKIPS
assert(EBI3['myText'],'EBI3["myText"] error'); // ERRORS
assert(EBI3.myText,'EBI3.myText error'); // ERRORS
assert(document.EBI4.myText, 'document.EBI4 error'); // ERRORS

alert('end of load');
}
</script>
</head>
<body onload="load();">
<form>
<input type="text" id="myText">
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top