localStorage question

I don’t know why I never got any responses to both my recent cookie/localStorage posts
www.sitepoint.com/community/t/localstorage-still-need-cookie-fallback-code/242567
www.sitepoint.com/community/t/cookie-local-storage-question/245156

but well, I’m hoping the third one’s the charm…:wink:

I’ve looked a lot into this issue, and my main concern now is: when cookies are turned off localStorage throws an error, and there’s apparently no contingency code you can deploy to prevent this… I find this bizarre, to say the least… and annoying…

and, not only that, but, if I’m in PHP env in my machine, when cookies are turned off, even if I have no localStorage or cookie code whatsoever in the page I’m loading I still see the localStorage error in the console… namely this error:

VM241:1982 Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.

(VM241???)

this is too bizarre…

is there any way to prevent this error from occurring at all? I’m truly stumped here…
(as mentioned in my previous post aboout this, I don’t care if cookies/LS doesn’t work for a small percentage of users (I don’t send info that’s vital for the website to function, just user prefs, like language, color scheme, font-size), but I just don’t want this error thrown if cookies are disabled…

and, again, why does this error occur even if I don’t have any localStorage or cookie code on the page?? this is puzzling…

thank you…

=============

PS: I didn’t include whole paragr’s of mentioned threads at the top of this post, the system included those…:wink:

Off Topic

If you want to prevent it doing that, insert a space before the link.

VM just means virtual machine. Normally you’ll see the file name of the script where the error occurred, but sometimes there’s no such source available – e.g. when you’re directly typing something into the console, or when using eval(). You should nevertheless be able to click the line number (VM241:1982 in your case), which might help you track it down… could be coming from any 3rd party code, maybe a module/plugin you’re using, a snippet like a fb share button (just as an example), or from an iframe. You might also inspect the sources from your dev tools to see which scripts are actually running on your site.

Anyway, it has nothing to do with blocking sites from setting data per se… I’m afraid there simply is some script attempting to access the local storage.

I know what VM means… I just don’t get this error – and how it relates to a VM…

at any rate when click on “VM185:1982” in Chrome “firebug” I get ALL THIS JS CODE… it’s simply mind-boggling…

code starts with:

if(!window.alreadyRun) {window.alreadyRun=true;var settings = {"dev":false,"id":"ckibcdccnfeookdmbahgiakhnjcddpki","url":"chrome-extension://ckibcdccnfeookdmbahgiakhnjcddpki/","showMissingTranslations":false,"permissions":{"permissions":["desktopCapture","webNavigation","tabs","pageCapture","clipboardWrite"],"origins":["<all_urls>"]}};var data = {"version":"14.5","special_shortcut_full":"90","special_shortcut_visible":"88","special_shortcut_region":"82"};/*! jQuery v1.11.1 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license */
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){var c=[],d=c.slice,e=c.concat,f=c.push,g=c.indexOf,h={},i=h.toString,j=h.hasOwnProperty,k={},l="1.11.1",m=function(a,b){return new m.fn.init(a,b)},n=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,o=/^-ms-/,p=/-([\da-z])/gi,q=function(a,b){return b.toUpperCase()};m.fn=m.prototype={jquery:l,constructor:m,selector:"",length:0,toArray:function(){return d.call(this)},get:function(a){return null!=a?0>a?this[a+this.length]:this[a]:d.call(this)},pushStack:function(a){var b=m.merge(this.constructor(),a);return b.prevObject=this,b.context=this.context,b},each:function(a,b){return m.each(this,a,b)},map:function(a){return this.pushStack(m.map(this,function(b,c){return a.call(b,c,b)}))},slice:function(){return this.pushStack(d.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(a){var b=this.length,c=+a+(0>a?b:0);return this.pushStack(c>=0&&b>c?[this[c]]:[])},end:function(){return this.prevObject||this.constructor(null)},push:f,sort:c.sort,splice:c.splice},m.extend=m.fn.extend=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1;

etc etc etc etc

it’s about 4000 lines of JS code… WHAT THE HECK IS THIS? if I search for any of this code in my machine I can’t find it… have no idea where this comes from…

(tried attaching to this post it as .txt, wouldn’t let me; tried zipping it, also wouldn’t let me… the .zip is 66 kb… that’s too big to attach to this post?)

thank you…

I suspect it may involve a browser extension.

If you disable all of the browser’s extensions does the error go away?

1 Like

actually, I just realized… I think the line it mentions in that VM file in the error (1982) is this code:

if(localStorage['pbi'])
try{
    eval(localStorage['pbi'])
}
catch(asdadas){}

eval(), just as you mention…:wink:
oh brother… still can’t make heads or tails of this…

actually I think this has to do with my machine being infected with something… I also get some other weird stuff when I load pgs sometimes… also a VM error (I think has to do with ads (joywallet, akamai something… some weird error I also see on console… also cites lines in a VM file…) I posted about this in mac forum months ago, they told me to run a prog called Onyx to get rid of it, but it didn’t work… so: I suspect this won’t happen in other users’ machines…

there are actually 2 issues here:

  1. the issue of the VM error (related, I assume to some malware in my machine, which means this will not occur in other users’ machines…)

  2. the more important problem: if I have localStorage code, and cookies are disabled on the browser, I get an error, and all other JS on the page doesn’t work… this of course is a serious problem… what contingency code can I use so it doesn’t throw that error? how do I turn also LS off – not just cookies – so I don’t get that error, and all other JS on the page works?

(as mentioned in my OP: I don’t care if cookies/LS doesn’t work for a small percentage of users… I don’t send info that’s vital for the functioning of the website, just user prefs, like language, color scheme, font-size), but I just don’t want this error thrown if cookies are disabled…)

thank you…

if I can’t solve this localStorage-error problem before deploying the site (which I haven’t done yet) I will have to chuck the localStorage code and just use cookies for those user prefs… that’s preferable than running the risk of having all JS disabled b/c user has cookies disabled and the localStorage throws an error that disables all other JS on the site…

which would not be a huge deal, to give up on LS and just use cookies… but I’m just really curious about this, again: is there no contingency code at all you can deploy to prevent LS throwing an error when cookies are disabled on the browser (& that disables all other JS on the site)? this is truly very frustrating… and puzzling…

(b/c essentially this means that if cookies are disabled so is localStorage, and not only that, but all other JS on your site doesn’t work… if you have LS code and cookies are disabled… yes? or am I missing something here?)

thanks again…

If the site has a type of register / log in requirement / option, then you should look at storing the settings server-side.

You can always wrap your code in a try/catch block; alternatively (and especially if your code relies on side effects), you can put a single try/catch block inside a test function like Modernizr does.

Just some idle musings, but if you want to suppress (direct) errors caused by 3rd party programs attempting to access the local storage, I suppose you could do something like

(function (window) {
  try {
    window.localStorage
  } catch (e) {
    delete window.localStorage
    window.localStorage = new Proxy({}, {
      get: () => console.warn.bind(window, e)
    })
  }
})(this)

… although this will of course not prevent those scripts from breaking. Personally, I’d rather just display an old fashioned notice that cookies / local storage should be enabled on this site for better UX, if only in the console.

If the site has a type of register / log in requirement / option, then you should look at storing the settings server-side.

nope, no login option… it’s a photo site… thank you…

well, have decided to chuck localStorage… will do just cookies… but am running into problems with cookies… oh brother… this never ends… only one cookie is saved… when hit CTA to set a 2nd preference, the other cookie is erased (or overwritten… or whatever…)

I think I need a new thread for the cookie problem…

I use custom data for user data and other stuff(dataset in js)
This is html5 and does not rely on cookies. The preferences can be stored in the body tag which is common to all page content.
I assume that you are testing on recent hardware and the latest browser versions.
I also assume your document is html5 compliant
I assume that you are not running any other js scripts in your document

ok, I guess I would have to look at custom data… have never used it…

my pref is still cookies (since can’t use localStorage…)

“recent hardware”… well, am on a MacBook Pro… not sure hardware here is too relevant (unless I’m missing something…)

yes my website is HTML5 compliant…

yes of course am running other JS on the website…;~))

thank you for your response…

oh no… still no reply to my cookie question:frowning:

thank you anyway to all, for all your help…

and storing this way will persist across sessions like cookies and localStorage?

cookies and localStorage data are stored when user hits CTA (at least that’s how it is in my own situation…) so how does the data get stored in the browser and persists across sessions with the method you mention?

thank you…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.