When using my webpage (http://localhost/mypage.html) accessing localStorage issues a security error:
Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
It is just a simple line
res = localStorage.getItem(name);
and even
console.log(localStorage);
issues the same error.
But it is a LOCAL site, so no cross domains are used.
What goes wrong here ?
This problem is related to a Chromium bug which is now fixed. See https://community.brave.com/t/html5-localstorage/100843
You can check if your current version is affected with this JSFiddle : https://jsfiddle.net/6sm54/2/
function lsTest(){
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
Related
I want to create a project with localstorage, but when I save it with setItem error show up like this:
Uncaught (in promise) TypeError: localStorage.setItem is not a
function
This is my code:
var dataPerson = {
name: "Jhon",
old: 20,
family: "Doe",
};
console.log(dataPerson);
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("data", JSON.stringify(dataPerson));
} else {
alert("Sorry, your browser does not support Web Storage...");
}
I use jQuery v3.4.1, Thank you
Can you try invoking localStorage from the window object...
window.localStorage.setItem("data", JSON.stringify(dataPerson));
This worked for me.
Check your code again if you've redefined the localStorage on somewhere else.
The code is completely working.
https://stackblitz.com/edit/js-jpzvta?embed=1&file=index.js
from https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage:
Exceptions
SecurityError
The request violates a policy decision, or the origin is not a valid scheme/host/port tuple (this can happen if the origin uses the
file: or data: scheme, for example). For example, the user may have
their browser configured to deny permission to persist data for the
specified origin.
so you should envelope your code with a try-catch, that will solve your
Uncaught (in promise) TypeError: localStorage.setItem is not a
function
in https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API you have an example of it, and given your particular exception, I'd start by
Feature-detecting localStorage
To be able to use localStorage, we should first verify that it is
supported and available in the current browsing session.
I work on a script that is added to hundreds of different websites in ways that I can't always predict, particularly when code is loaded in an iframe. In my code I've got a try/catch block setup to handle the exceptions. It's something like this:
var vals = {};
try {
vals.hostname = window.top.location.hostname; // will throw DOMException if loaded in iframe
< more code which might throw other exceptions >
} catch (e) {
if (e instanceof DOMException) {
vals.hostname = window.location.hostname;
} else {
<do something different>
}
}
e instanceof DOMException works awesome on Chrome, but it turns out that on Firefox and Safari, e instanceof DOMException is false on those browsers. I've done a lot of searching and for some reason can't seem to find anybody who explains how to check the exception type in a browser agnostic way.
Edit: Okay, Chrome is throwing a DOMException, but Firefox (and presumably Safari) are just throwing a generic "Error":
Error: Permission denied to access property "hostname"
I think that means I can't do anything with the specific error on other browsers.
I am unable to use "VisibleOnScreen" property on an Web element in test complete tool.Error is inconsistent, sometimes it works fine.
Error:Permission denied
Error location:
Eg:-
var obj = Sys.Browser("*").Page("*").FindChildByXPath(xpath);
if(obj!=null) {
if(obj.VisibleOnScreen) {
Log.Message("Object is visible on Screen");
}
}
I get the following error and iPad but not in desktop browsers:
JavaScript: Error
undefined
TypeError: 'undefined' is not a function
This is a larger js application, and this error message is totally unhelpful. Is there any way I can get the line number of the error or anymore information?
Update: This just got funky.
line : 0
page : undefined
desc : TypeError: 'undefined' is not a function
chr : undefined
Did the user agent spoofing in FF and safari. No error.
You could try registering a custom error handler to window.onerror
window.onerror = function (desc,page,line,chr)
{ alert('Line:'+line); }
desc = Error message
page = File/Page where the error occured
line = Well...
chr = Character position of the error in the line
If you bind an error handler to window.onerror, it should give you the line number, e.g.
window.onerror = function(msg,url,line) {
alert('The error is on line '+line);
}
This question: Debug JavaScript errors on iPad seems to indicate you can enable debugging too.
If the script is loaded dynamically, though, it can be hard to get such info in any environment.
I am trying to access the localStorage of a page from a Firefox extension. My understanding is that content gives a reference to the window of the current page. When I try and access the localStorage for the page with content.localStorage, I think I am getting a reference to it. However, when I try content.localStorage.length, I get nothing.
Attached is the code in question.
var myExtension = {
init: function() {
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent)
appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true);
},
onPageLoad: function(aEvent) {
var doc = aEvent.originalTarget;
alert(content.localStorage) // alerts "[object XPCNativeWrapper [object Storage]]"
alert(content.localStorage.length) // alerts nothing
}
window.addEventListener("load", function() { myExtension.init(); }, false);
EDIT#1: More information.
try{
alert(content.localStorage.getItem('todoData'))
alert(content.localStorage.length)
} catch (e){
alert(e)
}
The length is throwing the exception "[Exception... "Component is not available" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"
localStorage.length works when I have it on a standard web page in Firefox, but content.localStorage.length dose not work from the Firefox extension. Now I'm confused...
From a Firefox extension, you can access the localStorage object with window.content.localStorage, for example:
var ls = window.content.localStorage;
ls.setItem("myvariable", "myvalue");
var item = ls.getItem("myvariable");
Anything else will give you a "Component not available" error.
As an aside, globalStorage doesn't work this way. You can't use it at all with an extension because the object is only available if it's run from a server.
Using NsIDOMStorageManager xpcom interface you can get your local storage information.
https://developer.mozilla.org/en/XPCOM_Interface_Reference/NsIDOMStorageManager
use content.localStorage.wrappedJSObject.myVariable