4

In Internet Explorer 10, I have the following:

  /* 
   *   Alias document.getElementById(), to reduce typing, improve
   *   readability and speed up access to the function.
   */
   var getElmById = document.getElementById;

   function . . . (ctlID) {
       var ctrl = getElmById(ctlID);  // <————<<< error raised here
       . . .
   }

This has been working fine, but suddenly is giving me

SCRIPT65535: Invalid calling object

I have determined that if I check the box, Tools > Compatibility View Settings > [_] Display intranet sites in Compatibility View, the aliased function runs just fine, but if I clear that box, I get the error.

What is the reason? What is the specific issue that IE is responding to? Has aliasing functions like that been eliminated? Has there been some change to the way the 'document' object behaves?

Brian Wren
  • 367
  • 5
  • 15
  • This topic has an outstanding explanation: http://stackoverflow.com/questions/1007340/javascript-function-aliasing-doesnt-seem-to-work – Brian Wren Feb 05 '15 at 19:24

2 Answers2

2

I just posted this as an answer, but I don't see it. Trying again...

This topic has an outstanding answer:

JavaScript function aliasing doesn't seem to work

Community
  • 1
  • 1
Brian Wren
  • 367
  • 5
  • 15
0

"getElementById()" is supposed to be called as a method and not as a function, i.e. with a context to be applied upon (the document, or to put it in Javascript terms, the "this" value), obtained by parsing the left-side of the expression ("document.").

In the past, the context was necessarily accessed as a free variable from the function, so maybe they decided to clean out their code. Since some other major browsers do not support this kind of aliasing, I guess they'd figure most people wouldn't mind.

Elegie
  • 340
  • 1
  • 5
  • 16
  • I believe this is incorrect. It is a function, as it returns the referenced element. – Brian Wren Feb 05 '15 at 19:22
  • Hello. I'm not sure I follow you, sorry. The nature of getElementById is a function, for sure (it's not a string or a number). However, it is a member of the Document interface, as per the W3 DOM Core Level 2 specification, which means it should be called in relation to a Document instance. To put it differently, your alias would work only in the current window, but wouldn't be usable, would you like to retrieve an element from another document (say, in an iframe). – Elegie Feb 05 '15 at 19:54