freed script with hasOwnProperty

跟風遠走 提交于 2019-12-24 12:44:01

问题


I have a page containing an iframe in which I expect that within the iframe the user will navigate to many different pages - all of them in the same domain as the parent page.

In the top level window I have a persistent object, let's call it appData. And in the iframe I often have lines like

parent.appData[someProperty] = {a : 1, b : 2};

I'm aware of the general problem of freed script errors and I understand that I shouldn't try to call any arbitrary methods on such an object since the originating document may have unloaded, but surely hasOwnProperty shouldn't be such a method, and I should be allowed to say, in some subsequent child page:

if (parent.appData[someProperty].hasOwnProperty('a'))

Shouldn't I? And here's what's odd: it works as I expect in every browser I've tested except MSIE 10. I did see the recent answer to IE9 "Can't execute code from freed script" when calling hasOwnProperty() and indeed using in seems to work fine for my case, but I'm wondering whether I've been "cheating" all along or if this is a bug in MSIE 10.

jsFiddle doesn't handle iframes so I'm not sure how best to demonstrate this with an example, sorry.


回答1:


Personally, I'd handle all functions similarly if you are concerned about the freed script issue, including built-in ones such as hasOwnProperty. In that specific case, it still could technically be interpreted as a function on an object, and apparently some browsers treat it as such (IE10) and others treat it as a special case that is still accessible (the others you mention). Even if it worked in all browsers, I'd still feel wrong about it.

Thus, not sure about your exact case, but could you instead do something like:

var appData = parent.appData || {};
var someProperty = appData[someProperty] || {};
if(someProperty[a] !== undefined){
    ..
}

Otherwise, you could always surround it with a try..catch

Update
And of course you could use in as well, as mentioned in your linked question.



来源:https://stackoverflow.com/questions/14461506/freed-script-with-hasownproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!