0

How to make JS change the content (HTML/CSS) of a website randomly and make it storage in cache?

Lets suppose I have two HTML blocks I want to display in a page. One is a red with number 1 inside, and the other is a green with number 0 inside. And I want to make it display randomly on page load, one time the green , the other time the red .

I am starting with JavaScript, and my knowledge is still very shallow

Thanks in advance

4
  • 1
    You're asking multiple questions. Scope it down to a single question. And if you have made any attempt at this, show us your attempt. Commented Mar 26, 2019 at 15:44
  • 1
    Welcome to SO ;) Please read this article on how to ask a good question. This would include a proper description of what you are trying to achieve, your code (or the relevant snippets) as well as your efforts showing what you have tried so far and possible error messages. It is also advisable to provide a full MCVE.. Commented Mar 26, 2019 at 15:47
  • Eo you mean storage like cookies or local storage? Commented Mar 26, 2019 at 15:48
  • I mean storage like cookies Commented Mar 26, 2019 at 16:45

2 Answers 2

2

Let's start with an HTML element to display:

<div id="result"></div>

And some CSS to make it a square:

#result {
  width: 10em;
  height: 10em;
}

To manipulate this in JavaScript, we can:

var myValue = localStorage['myKey'] // try to access our stored value

if (!myValue) { // our stored value did not exist
  myValue = Math.random(); // get a number in the range [0, 1)
  localStorage['myKey'] = myValue; // save our value to storage
}

display(myValue);

function display(v) {
  var color;
  var num;
  if (v > 0.5) {
    color = 'red';
    num = 1;
  }
  else {
    color = 'green';
    num = 0;
  }  
  var myDiv = document.getElementById('result') // get our div
  myDiv.innerText = num; // set the text to 1 or 0
  myDiv.style.backgroundColor = color; // full list of properties here https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats awesome, Ethan! Let's say I want 2 completely different blocks to be displayed (instead of red or green <div>), I mean, with different content, like texts and img, etc inside them. Them I guess I should create should use JS to add a class that makes "display: none" to them. Right?
1

do this for your solution

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
    .page {
        width: 200px;
        height: 200px;
    }
</style>
<div class="page"></div>


<script>
    var pageMeta =new Array([],[]);
    pageMeta[0]['data']='page 0';
    pageMeta[0]['color']='red';
    pageMeta[1]['data']='page 1';
    pageMeta[1]['color']='green';

    var curentPage = localStorage['curentPage'];
    if (!curentPage) {
        curentPage=Math.floor(Math.random() * 2);
        localStorage['curentPage'] = curentPage;
    }

    page(curentPage);

    function page(a) {
        $(".page").html(pageMeta[a]['data']);
        $(".page").css('background-color',pageMeta[a]['color']);
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.