Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / June 2006



Tip: Looking for answers? Try searching our database.

shortcut for document.getElementById and some error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HopfZ - 30 Jun 2006 16:57 GMT
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?
Martin Honnen - 30 Jun 2006 17:19 GMT
> I made two shortcut functions for document.getElementById as:
>
[quoted text clipped - 5 lines]
> 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.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Randy Webb - 30 Jun 2006 17:34 GMT
HopfZ said the following on 6/30/2006 11:57 AM:
> I made two shortcut functions for document.getElementById as:
>
[quoted text clipped - 4 lines]
>
> 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:
[quoted text clipped - 6 lines]
>
> Is EBI3 a bad way?

Yes.

gEBI might be a better name for them though.
Signature

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/

Evertjan. - 30 Jun 2006 17:36 GMT
HopfZ wrote on 30 jun 2006 in comp.lang.javascript:

> function EBI2(id){return document.getElementById(id)};
> var EBI3 = document.getElementById;
[quoted text clipped - 3 lines]
> EBI2('myText'); //works
> EBI3('myText'); //unexpected error

Think & try:

r = EBI3['myText']

or:

r = EBI3.myText

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

HopfZ - 30 Jun 2006 18:39 GMT
> HopfZ wrote on 30 jun 2006 in comp.lang.javascript:
>
[quoted text clipped - 13 lines]
>
> 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>
Evertjan. - 30 Jun 2006 19:04 GMT
HopfZ wrote on 30 jun 2006 in comp.lang.javascript:

>> r = EBI3['myText']
>>
[quoted text clipped - 3 lines]
>
> 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);}
[quoted text clipped - 5 lines]
>   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');
>}
[quoted text clipped - 3 lines]
> <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>

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.