473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

shortcut for document.getElementById and some error

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
5 4408


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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
4
by: noddy | last post by:
I need to retrieve some notes from a database, allow users to modify them and then return them to the database.I have done this satisfactorily using php.However I would like to do it using AJAX. ...
4
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
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...
0
agi2029
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.