Save data in Localstorage and then check if it exists in another page

Asked

Viewed 570 times

0

Good afternoon person. I need to save a data in Localstorage and then check if this value exists on another page, I tried here but it didn’t work.

First I define the value of the "variable" Prof_amc

if($(myEmail == singleMail)){
            if(approved == true && categoria == 'PLF'){
                // Cria um item "Prof_AMC" com o e-mail do usuário
                window.localStorage.setItem('Prof_AMC', singleMail);
                window.location.href = document.referrer+'?sc=2'; // Parametro URL Politica PLF
            }

Then on another page I try to check if it exists and if the SC parameter is different from -1:

if(window.location.href.indexOf("sc")!=-1 && localStorage.getItem('Prof_AMC')){

}
  • vc gave a console.log in this result localStorage.getItem('Prof_amc') to check what’s coming?

  • Type localStorage.getItem('Prof_amc') in Chrome console? If it is an error Uncaught Domexception: Failed to read the 'localStorage' Property from 'Window': Access is denied for this Document. at <Anonymous>:1:1

  • The two pages that access Localstorage are on the same domain or is a redirect external?

  • tried in another browser?

  • same domain, it was my mistake here so I wasn’t giving. I tried now and the result was null, IE, is not passing the variable to localStorage

  • I also tried in firefox and tbm gave null it seems that is not passing anything from one page to another

  • try the solution I posted there, if it doesn’t work, go in your code and right after you give setItem in localStorage, use the js console.log(localStorage) command and see if you’re saving

Show 2 more comments

2 answers

0


It worked when I did it this way:

if($(myEmail == singleMail)){
            if(approved == true && categoria == 'PLF'){
                // Cria um item "Prof_AMC" com o e-mail do usuário
                window.localStorage.setItem('Prof_AMC', ""+singleMail+"");
                window.location.href = document.referrer+'?sc=2'; // Parametro URL Politica PLF
            }

0

Your browser is blocking access to localStorage. To resolve this, go to your browser content settings and allow third-party websites to save cookies and information. see here the path on Chrome (in English)

  • The problem is that it doesn’t work on Chrome, firefox, or any browser. So I don’t think it’s the browser.

  • I just discovered that the problem is not in localStorage but rather that it is not allowing me to save a variable in it. If I just put "test" then it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.