0

my goal is to hide the content of my homepage when someone visits. onClick to begin button the content should be shown. Content should stay open when user goes to other page and comes back to homepage. But it will be hidden when user closes the window and opens up the homepage again. To achieve this goal I have put the following code but it keeps the content open even when user closes and opens the window. So please help me out.

if (! localStorage.noFirstVisit) {
    // hide the element
    $("#content").hide();

    // check this flag for escaping this if block next time
    localStorage.noFirstVisit = "1";
}

Another issue is when the content shows the design gets little messed up(by widening the divs, bringing horizontal scroll)

$(".roll-button").click(function(){
    $("#content").show();
});

I would highly appreciate if you check website, suggest me fix or show me proper way to achieve this goal. url:iamicongroup.com

3 Answers3

1

You can totally use sessionStorage to detect whether it is new tab(window) or not.

When you first visit this page, set sessionStorage.noFirstVisit = "1";.

After you go to another page and back, sessionStorage.noFirstVisit is still "1".

But when you close the tab or browser and open the page newly again, sessionStorage.noFirstVisit will be undefined.

Documentation is here

The documentation also provide the difference between sessionStorage and localStorage.

Terry Wei
  • 1,521
  • 8
  • 16
0

I would suggest reading this: Detect Close windows event by Jquery

It goes over window unloading (beforeunload), which I believe is what you're after. You can set/unset your localstorage values there based on certain criteria being met; for example:

$(window).on("beforeunload", function() { 
    if(localStorage.noFirstVisit = "1" {
        // do something
        localStorage.noFirstVisit = "[someValue]"
    }
    else {
        // do something
        localStorage.noFirstVisit = "1"
    }
})
Drew Adams
  • 154
  • 3
  • 11
0

Another issue is when the content shows the design gets little messed up(by widening the divs, bringing horizontal scroll)

how about adding something like 'ng-cloak' in angular, to avoid the undesirable flicker effect caused by show/hide.

when clicking the roll-button, it prevents the divs from showing unfinished..

Steven J
  • 63
  • 7