1

I have a homepage with a list of puzzles as links, and then each individual puzzle has its own webpage. Upon entering the correct password on a puzzle page, I would like a) a window alert to appear confirming that the password was correct, b) to add the link to the next puzzle to the homepage, and c) redirect to the homepage.

I've got the window alert and redirect down, I just don't know how (or if it's even possible) to make alterations to the homepage in this situation.

Currently that if statement looks like this:

if (this.document.login.pass.value == password) {
    window.alert("Correct password.");
    top.location.href="home.html"; 
}

Previously they just had the password form on the homepage, and of course that worked just fine, where content1 is the link we wanted to add to the page.

if (input == password1) {
    var x = document.getElementById("content1");
    x.style.display = "block";

Is it possible to accomplish this from another page? I'm working with someone else's code and I'm a little lost, so thanks for any help.

  • 4
    When redirecting back to the homepage, you could include the next puzzle as a query string parameter, e.g. `https://your-site.com/index.html?nextPuzzleUrl=/puzzles/puzzleX`. You could then have JavaScript on your home page that checks for (and uses) `nextPuzzleUrl` each time the page loads. There are other methods but this would be among the simplest. It's made even easier by [`URLSearchParams`](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters). – Tyler Roper Mar 18 '19 at 20:26
  • Welcome! When you say 'website', what do you mean *exactly*? It is an .html file? Maybe a php/node/java/whatever controller in a web-server? It is a page as in a front-end framework like Angular or React? – Sergeon Mar 18 '19 at 20:27
  • Or you could use something like jscookie. You could give a user a cookie when they enter the password correctly. Then on the hope page you could show or hide links based on whether or not the user has a cookie. `if (Cookies.get('~puzzle1') == 'true')` – jarrodwhitley Mar 18 '19 at 20:31
  • @Sergeon Hi, thanks for your comment! It's just an .html file. And eventually everything will be hosted on CPanel. – user11222836 Mar 18 '19 at 20:40

1 Answers1

0

You can either use a URL hash, or a cookie.

  • Hash:

On puzzle page: window.location.href='homepage.html#to-test-4'

On homepage: alert(window.location.hash)

This will, of course, be very visible to the user, because it shows up in their address bar, so they might do some cheating. The next method is more discrete.

  • Cookie:

See examples at https://github.com/js-cookie/js-cookie

Dinu
  • 1,374
  • 8
  • 21