1

I have a function defined on the window object somewhat like this

window["functionName"] = function(){
    // code here
};

Now whenever I need that function I just call

window["functionName"]();

This works well in all browsers except IE8. IE8 throws an error

SCRIPT438: Object doesn't support property or method 'functionName'

I googled for explainations but didnt find any.

EDIT: After a long time debugging I got the cause of the error

Actually the above function definition was inside another function.

function otherFunction(){
    window["functionName"] = function(){
        // code here
    };
    // code here
}

When I moved it outside it looked to work fine.

window["functionName"] = function(){
    // code here
};

function otherFunction(){
    // code here        
}

But I still cannot understand why this weird behaviour?

Mandeep Jain
  • 2,304
  • 4
  • 22
  • 38
  • Can you link to a test case? – MasterAM Jun 29 '13 at 14:12
  • well i setup a test case here and surprisingly it is working very well here http://jsfiddle.net/C9g8H/. Is it because my code is modular and distributed accordingly using require js? – Mandeep Jain Jun 29 '13 at 14:27
  • Is it possible that there is a node with the id that you wish to use to hold the function? There are some 'protected'\read only properties that will trigger this type of error in earlier IEs. – MasterAM Jun 29 '13 at 15:59
  • @MasterAM, got the cause of the error. Edited my question – Mandeep Jain Jul 01 '13 at 05:55
  • I do not have any machines with IE8 at the moment, so I cannot test this matter further. Why are you using window's global namespace anyway? Would it not be better to narrow down your use of namespace to a single global name that would hold all other global variables? – MasterAM Jul 03 '13 at 09:54
  • Well, the current code was wired that way and I did not want to change that – Mandeep Jain Jul 03 '13 at 11:39

0 Answers0