Skip to content Skip to sidebar Skip to footer

Javascript: Illegal Invocation

I am experiencing an issue. I am working with a fairly old application which has the following logic: var Page; var OriginalSubmit; function init() { Page = document.forms[0];

Solution 1:

When you assign OriginalSubmit = Page.submit you're obtaining a reference to the .submit() function, but when you call it as just plain OriginalSubmit() you're losing the Page context variable that will be passed as this to that function:

myObject.method();  // calls "method" with "this === myObject"

var method= myObject.method();
method();           // calls "method" with "this === window"

Instead, use this:

OriginalSubmit.call(Page);    // sets "this" to "Page"

Note that even then, it's possible that re-assigning a method of a native DOM element won't work portably in all browsers, and may not work at all.

Post a Comment for "Javascript: Illegal Invocation"