The problem is that the functions do not seem to be able to see each other.
For Example:
unsafeWindow.helloworld = function() {
alert('Hello world!');
helloworld2();//fails with error saying it does not exist
}
unsafeWindow.helloworld2 = function() {
alert('Hello world!2');
helloworld();//fails with error saying it does not exist
}
insert("helloworld();"); //appends a script element to the body
Both functions can be called using insert or firebug console but they do not know about each other internally? What is the problem here?
It would help a lot if you format your code:
You have in infinitely recursive funciton – helloworld calls helloworld2 which calls helloworld and so ad infinitum.
But anyway, you are setting a property of
unsafeWindow:but then attempting to call it using an unqualified identifier that is resolved on the scope chain:
So the identifier
helloworldis resolved on the scope chain of the execution/variable object created whenunsafeWindow.helloworld2is called.So
helloworldis set as a property ofunsafeWindow. When the function is called, the identifierhelloworld2will be resolved using the scope chain of the function.In browsers, the window/global object is on the scope chain so variable names may be found there (i.e. variables may resolve to properties of the global object if not found sooner in the scope chain), but I suspect that when
unsafeWindow.helloworldis called, that its scope chain ends with the document’s global object, notunsafeWindow. Otherwise calls to functions in the document would also haveunsafeWindowon their scope chain, which doesn’t seem right to me.Or I might be completely wrong about that. 🙂