-1

I have a banner that closes with an onclick function that l created on a button

//button code where onclick is stored

<button id='close'onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</button>

// js Code

        window.onload = function(){
        document.getElementById('close').onclick = function(){
        this.parentNode.parentNode.parentNode
        .removeChild(this.parentNode.parentNode);
        return false; </script>

Now, how do l create a local storage for this function, that when someone refreshes the page the banner would be closed if the person did close it before?

*please excuse my language, not my primary one Also, terribly sorry if l cannot explain correctly my issue

Thanks

  • 1
    You don't _"store an onClick function to localstorage"_, that makes little sense to begin with. What you should store, is the _information_ that the user already closed the banner. And then you have your function read the information from localStorage, and let it decide what to do based on that. – CBroe Aug 17 '22 at 10:42
  • any examples on that ? – Noed Johnson Aug 17 '22 at 10:43
  • https://stackoverflow.com/questions/55858249/how-to-hide-banner-after-click-and-save-with-localstorage – CBroe Aug 17 '22 at 10:45
  • https://stackoverflow.com/questions/41897449/show-slide-in-banner-only-one-time – CBroe Aug 17 '22 at 10:45
  • thanks a lot , first solution worked, but with a catch , after refreshing , the banner still stays for couple of ms then closes , any idea why man ? – Noed Johnson Aug 17 '22 at 11:01
  • Because of _when_ `window. onload` actually fires. – CBroe Aug 17 '22 at 11:08
  • any solution for this one ? – Noed Johnson Aug 17 '22 at 11:09
  • Put the script at the end of body and let it execute immediately instead; or use the document ready event instead of load (https://stackoverflow.com/a/4395858/1427878); or turn the logic around - have the banner hidden via CSS initially, and then decide whether you have to _show it_ in the first place, or not. – CBroe Aug 17 '22 at 11:17
  • ok the script at the end didnt work , l ll try the $.ready event since its onload. but instead $.ready l gotta use jQuery.ready since it shows some errors. l cannot play with the banner on css side sadly otherwise that would have been it. THANKS A TON MAN , I REALLY VALUE THIS. Sorry for explaining the issue so badly. – Noed Johnson Aug 17 '22 at 11:26

1 Answers1

0

What you're probably looking for is Cookies.

To store a variable or any string

document.cookie = "clicked=false"; //or true if clicked

to then access that cookie or data later on use

let decodedCookie = decodeURIComponent(document.cookie);

this will return your cookie string, you can then parse this and retrieve actual value.

Brigapes
  • 68
  • 7