Problem emulating localStorage in IE7

I have an application with a top frame and two sets of frames inside this top frame. When navigating other pages, the top frame remains unchanged and only frames change. I use localStorage to store some data (which is also stored on the server, but if it is on the client, we do not do it back and forth each time), this function is not available in IE7, so we decided to try to simulate localStorage in IE7.

The idea was to store the localStorage variable in the top frame (only if localStorage was not available). Whenever localStorage was not available in the top frame, we then created a dummy localStorage object with the parameters _data, getItem (), setItem (), removeItem (). The life of this object will continue as long as life is in the upper frame, which would save us a lot of round trips to the server and, therefore, offers a big performance boost in IE7.

The problem I am facing is that whenever I change the frame (and not the top frame), I get localStorage from the top frame and try to get the element using window.top.localStorage.getItem ('... ); I get an error that the code cannot be executed from the freed script.

Any ideas why I get this?

+4
source share
3 answers

Perhaps I will repeat what Brilland has already shared, but since I do not fully understand his answer, I will also share some ideas and tips:

  • First of all, make sure that all relevant frames are hosted on the same domain (including things like opening the top frameset at www.domain.com and having internal links without "www.").
  • If you need cross-domain scripting, check out the internet for some relevant tricks to achieve this ( theory ).
  • It may be worth trying to wrap all access to the local storage in several functions in the top frame (I can come up with several reasons that could theoretically cause problems with direct access to objects in the top frame ... although this should work, as far as I know).
  • Try reinitializing all the links to the subframes, for example, you might forget to add “var” before the window.frames link and not reinstall it after navigation (this would explain the error message, although this seems like a pretty unlikely error).
  • It may also be worthwhile not to specify links to top.localStorage inside any of the child frames (although, again, this should not cause any problems).
  • You might want to use the cross-platform localstorage shell. They usually work with proprietary functions in IE , which allows you to use a local function in IE (1MB max, but this should be enough). An example of such a library is store.js
+3
source

I would recommend you take a look at jStorage , which provide a common interface for localStorage or the old globalStorage or userData . You can probably use jStorage directly (see here ) and save your time by writing the appropriate code, or you can use the same idea for your own implementation. You can find more detailed information about the behavior of old userData, for example here .

+4
source

See What causes the error “Unable to execute code from a freed script”

This error message occurs when accessing the code created by the child frame (which was closed). This means, at the very least, that you cannot support JavaScript functions after the window from which they were received has been closed; I'm not sure if this applies to data objects, but it is possible. If so, simply storing JavaScript objects in the top window will not work.

Perhaps you can work around this problem by making sure that you completely separate the data from the child window before saving it. This can be done using the storage function (which should only be created by the parent window). JSON encodes this data and stores the encoded string. The extraction will be less thin since the extracted object should not be stored longer than the child window that extracted it.

+2
source

Source: https://habr.com/ru/post/1412496/


All Articles