Localstorage not working on mobiles


After setting a code to display a simple message and having tested it successfully in computers and tablets, I see that it does not work on Android, Iphone or Windows mobiles.

<div id="box">
    <p>My message to be displayed just once on the landing page.</p>

(function() {
    var visited = localStorage.getItem('visited');
    if (!visited) {
        document.getElementById("box").style.display = "block";
        localStorage.setItem('visited', true);


It works like a charm in computer and tablet browsers.

Some clue please?

I have found a jQuery-based solution that works on mobiles:

Isn’t there a way to do it without jQuery?


Anything that can be done using jQuery can be done in JavaScript. I’m not the one to pick it apart for you, but I’m sure someone around here will be able to help with refactoring it.

The part of that solution that relates to localStorage is the same as in the prior version you posted - so the jQuery code has nothing to do with whether the storage commands are working or not. The only difference is that the jQuery version is set to run sooner as it only waits for the DOM to load and not all the other external files - you can get the same result with ordinary JavaScript attached to the bottom of the page if you djust run the code straight away instead of onload.

1 Like

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