0

I have the following code, I'm trying to determine which button a user presses.

function Navigation(navigating, timed) {
  if (navigating == general) {
    if (timed) {
      window.location.replace('general.html?timed=' + timed);
    } else {
      window.location.replace('general.html');
    }
  } else if (navigating == sport) {
    if (timed) {
      window.location.replace('sport.html?timed=' + timed);
    } else {
      window.location.replace('sport.html');
    }
  } else if (navigating == music) {
    if (timed) {
      window.location.replace('music.html?timed=' + timed);
    } else {
      window.location.replace('music.html');
    }
  }
}
<button type="button" id="general" onclick="Navigation(general, null)">General Knowledge</button>
<button type="button" id="sport" onclick="Navigation(sport, null)">Sport</button>
<button type="button" id="music" onclick="Navigation(music, null)">Music</button>
<button type="button" id="generalTimer" onclick="Navigation(general, 'true')">Timed General Knowledge</button>
<button type="button" id="sportTimer" onclick="Navigation(sport, 'true')">Timed Sport</button>
<button type="button" id="musicTimer" onclick="Navigation(music, 'true')">Timed Music</button>

It actually works, however when I get to the next page, I don't know how to get the timed variable to affect the page.

Nhan
  • 3,595
  • 6
  • 30
  • 38
TomBrad95
  • 31
  • 1
  • 7

3 Answers3

0
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Usage

Example URL:
http://www.example.com/index.php?id=1&image=awesome.jpg

Calling getQueryVariable("id") - would return "1".
Calling getQueryVariable("image") - would return "awesome.jpg".

Source: CSS-TRICKS

silentw
  • 4,835
  • 4
  • 25
  • 45
0

You can get the url params from window.location.search. This function will parse the values, and allow you to get them all or just a single value.

unfortunately it's not working in the debug window due to the ristrictions of the iframe.

function urlParams(key) {
  const search = location.search.slice(1)
    .split(/\=|\&/)
    .filter(val => val !== '')

  const obj = {}
  for (let ii = 0; ii < search.length; ii+=2) {
    obj[search[ii]] = search[ii + 1]
  }
  return key
    ? obj[key]
    : obj
}

console.log(
  urlParams('testing'), // get specific param
  urlParams() // get all params
)
synthet1c
  • 6,152
  • 2
  • 24
  • 39
  • You define the `obj` keyword as `const` and then you define a value to it later. It isn't valid, should be a `var` or `let`. – silentw Dec 02 '16 at 13:22
  • @silentw the variable is pointing to the object and that never changes, the properties of the object make no difference. – synthet1c Dec 02 '16 at 13:24
  • You are indeed correct. Sorry, I removed my down vote. – silentw Dec 02 '16 at 13:31
0

you can use localstorage for example: first save the value that has been choosen to localstorage by code like this localStorage.nav = navigating; and then for nextpages and whereever you want call the value with document.getElementById("result").innerHTML=localStorage.nav; for example write down it with

alifallahi
  • 332
  • 1
  • 9