0

I'm using this javascript for a click counter in my blogger blog:

    function clickCounter() {
if(typeof(Storage) !== "undefined") {
    if (sessionStorage.clickcount) {
        sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
    } else {
        sessionStorage.clickcount = 1;
    }
    document.getElementById("result").innerHTML = "Correct! " + sessionStorage.clickcount + " Smart answers 'til now.";
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support this quiz...";
}

}

<button onclick="clickCounter()" type="button">Suspension</button>

Is there any way to create something similar through a non javascript method?

Can you help me triger an event (extra text message through popup or within the page) every 5, 10, 20, 100 clicks?

Thank you very much

6
  • Do you have the ability to have a server side solution? Outside of that I am not sure it's possible. Commented May 27, 2016 at 18:12
  • No server side solution for me Brad. Thank you for asking. Commented May 27, 2016 at 18:15
  • So you want to manipulate the DOM without JS. Good luck. Commented May 27, 2016 at 18:17
  • Well Alon actually i was hoping more in some idea out of the box. Something from the scratch let's say. Not really manipulating the existing code. Commented May 27, 2016 at 18:24
  • @user3686107 Read Jeremy's answer - It explain exactly the limitation you're facing and why it's probably impossible to achieve (Unless you have JS and/or server side) Commented May 27, 2016 at 18:30

2 Answers 2

2

HTML, and the Web in general, was designed to be stateless.

When you pull up a page, it should be like the first time -- and every time -- you pull up the page.

Since then, people have come up with a number of techniques to add state -- to save data, but they all involved one of two methods -- or sometimes both.

Method 1: Store state on the server.

This method uses HTML forms or cookies to slip information to the server when you load and reload a page.

Method 2: Store state in the client

While there are some older versions of Internet Explorer that can be coded in VBA, we are going to ignore that. The only "real" way to run any kind of code on the client, to store any data, is to use JavaScript.

Method 3: Use the client to talk to the server

Using Ajax, you can let your client talk to the server, but without doing a page reload. This still uses JavaScript.

So, to answer your question:

  • Without a server
  • Without JavaScript

No, you cannot save or store anything.

Sign up to request clarification or add additional context in comments.

Comments

1

I have not tried this but...

What if you put multiple buttons positioned on top of each other. As each one is clicked, it can be made to vanish with something like

a:visited { display: none; }

The ones that need to display a message (5th, 10th, etc.) have different behavior attached.

See on click hide this (button link) pure css

3 Comments

Genius! (though probably impractical). Well done with a "non-javascript" solution none the less.
Thank you bmb that gave me a lot of ideas for other situations but what i forgot to mention regarding extra events when a certain number i reached is that clicks come from buttons in different pages and the result is shown in one div <div id="result"></div> at the top of the website. What i would really like is to have an extra event when the "result reaches 5, 10 etc.
I agree with JonSG, for any practical situation, this probably doesn't work. Jeremy's answer is more correct, although mine is an example of the client (sort of) storing something without javascript. Unfortunately, you can't retrieve that something. I'm upvoting Jeremy.

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.