Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback gracefully to using an object in memory to store data when localStorage is undefined #137

Closed
TotallyInformation opened this issue Jun 28, 2012 · 10 comments

Comments

@TotallyInformation
Copy link

Hi Oscar, we've been chatting over on Stack Overflow.

I've been trying to get Epic to work in IE9 but I can't do it. Your demo page works fine but I cannot run it locally (e.g. without a server).

Here is the code:

<html>
<head>
  <title>Testing Epic Editor</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script type="text/javascript" src="epiceditor/js/epiceditor.js"></script>
</head>
<body>

  <div id="epiceditor"></div>

  <script>

    $(document).ready(function()  {
      var editor = new EpicEditor({ basePath: 'epiceditor/' }).load();
    });

  </script>

</body>
</html>

Everything does load and it runs perfectly in Firefox 11. It does not run in IE9. It seems to be something to do with localstorage. The errors are:

SCRIPT5007: Unable to get value of the property 'epiceditor': object is null or undefined epiceditor.js, line 1135 character 5
SCRIPT5007: Unable to get value of the property 'epiceditor': object is null or undefined epiceditor.js, line 1000 character 5

Line 1135 is:

var files = JSON.parse(localStorage[this.settings.localStorageName]);

Line 1000 is:

storage = JSON.parse(localStorage[self.settings.localStorageName]);

In each case, the name is set OK but localStorage seems to be underfined. The problem seems to be at line 346:

    if (localStorage) {
      if (!localStorage[self.settings.localStorageName]) {

Because that first step fails on IE9 when loaded locally. I simply added an alert wrapped in an else {} to prove the point.

Would it not be better to put out an alert instead of silently failing for reasons unknown?

Of course, it would be even better if something could be done about this. Would it be possible to use a library instead of localStorage?

@OscarGodson
Copy link
Owner

IE9 has a security thing with localStorage. IE9 doesn't allow localStorage locally. Just put it on a server like WAMP or if you're VMing into Windows run Python's simple server (python -m SimpleHTTPServer) and then you can go to 192.168.x.x:8080 in IE9 on your VM and it'll load it from your *NIX box.

Are you just developing, or does this need to be run locally for some reason?

@TotallyInformation
Copy link
Author

Yes, I need to run locally, this is part of an HTML Application (HTA) - HTA's get special access to the local system but, as you say, some other things don't work. I need to access a bunch of markdown formatted files that will be on the local drive and I wont be able to run up a server due to restrictions in place on the PC's.

I've just made a quick amendment that I'm sure breaks all sorts of stuff but seems to work at first sight. I've added the following between loading JQuery and your script file:

  <script>
    var myStorage = [];
    if (localStorage) {
      myStorage = localStorage;
    }
  </script>

I then did a global search and replace of localStorage with myStorage! I didn't expect it to work but it seems to! I'm guessing bad things will happen when I go further but I only really need the editor and preview, I'll be doing the file handling myself

@OscarGodson
Copy link
Owner

You don't want that, you want:

if (!localStorage) { //if it doesn't exist use a backup
  localStorage = {}; //override the default localStorage object since it doesn't exist
}

This way in Firefox and Chrome it'll be able to store drafts in localStorage, but if it can't (IE9) it'll use a backup. It wont save on refreshes, obviously, but it'll work. Also you wont have to do a find and replace and upgrades will still work.

@TotallyInformation
Copy link
Author

I tried that first - sorry I should have said.

I got an error saying I wasn't allowed to redefine a constant. So it looks like IE9 knows that it is a reserved word.

Actually, you've got me thinking about web servers - I know that Java is available on all of the machines so I'm just looking to see if there is a Java equivalent of the Python suggestion - don't know why I didn't think of that since I'm often forced to use Java tools on these machines because it is the only thing I can actually run beyond what the brain-dead admins think should be run.

@OscarGodson
Copy link
Owner

I see, thats good to know. I'll change the title of your post and work on a feature to fallback gracefully in IE9 rather than just dying if run locally.

@OscarGodson
Copy link
Owner

@TotallyInformation also, when you're done i'd love a demo of it or a screencast and the source to see what you did with EE :)

@TotallyInformation
Copy link
Author

OK, thanks for the help.

I've not had much luck with the Java SimpleServer equivalent. As usual, Java proves frustrating. I could create something myself - if I were a Java programmer!

@OscarGodson
Copy link
Owner

FYI @TotallyInformation I'm going to implement a way to disable localStorage since I've had a couple people request this off GitHub and recently @levint from ticket #141. I want to get this in develop this week.

OscarGodson added a commit that referenced this issue Jul 4, 2012
… also makes it so EE wont crash when localStorage is undefined (IE7 and also when IE9 is using the file protocol)
@OscarGodson
Copy link
Owner

@TotallyInformation Could you pull develop and let me know if the new commit fixes your issue? If localStorage is undefined it writes to an empty object just like you did above. Also, while I was in there I added a boolean clientSideStorage option which you can forcefully make it not use localStorage by setting it to false.

I haven't added docs or tests for this yet, but all tests pass. If it breaks anything let me know!

@OscarGodson
Copy link
Owner

Added docs and tests, so closing, but feel free to reopen if you are still having problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants