By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

shortcut for document.getElementById and some error

P: n/a
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?

Jun 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a


HopfZ wrote:
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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 30 '06 #2

P: n/a
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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 30 '06 #3

P: n/a
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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 30 '06 #4

P: n/a
Evertjan. wrote:
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>

Jun 30 '06 #5

P: n/a
HopfZ wrote on 30 jun 2006 in comp.lang.javascript:
Evertjan. wrote:

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 <script type='text/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');
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>
</input> is not html
</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>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.