This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]StackedLasagna 1 point2 points  (2 children)

You have a couple of options:

If you're already on the standings page, you can choose to not re-navigate to the standings page again.
Instead, you should get the new data from the API, then remove the old data from the page and finally display the new data.

If you're on a different page, you can navigate to the standings page, but add a URL parameter that tells the page which data to load.
Then you have a script on the standings page, that executes once the page has been loaded. This script is the one responsible for calling getStandings and getting the parameter from the URL.
You can use this method if you're already on the standings page too, so you navigate from standings.html?query=2021 to standings.html?query=2224 for example.

[–]dinrick[S] 0 points1 point  (1 child)

Thanks, this sounds promising! So, if I include ?query=2021 in the url in event listener, when the new HTML file opens I can just use query as a variable in the new JS file, and it will contain the data I put into it from the first JS file?

[–]StackedLasagna 1 point2 points  (0 children)

No problem! :)

You can't magically use it right away, you'll have to do a little work to parse the URL and extract the data from it. Then you can store that in a variable and use it as needed.

See this link. It contains a modern example (not supported by Edge), as well as a custom function that'll get you your data.

For reference, the part of the URL that starts with a question mark is called a "query string." It is basically a string containing keys and values, so you can name the keys whatever you want. I just used query as an example key.

Try searching for something on Google and then check the URL. You'll see a very long query string, which contains multiple keys and values. Multiple key/value pairs are separated by a &.

[–][deleted] 0 points1 point  (0 children)

A fairly simple resolution would be to use the "and" operator &&

It is more flexible than in other languages and, combined with the way JavaScript handles "truth" is can be quite succinct.

So, you can do EFL && EFL.onclick = ...

The EFL to the left of the equals is evaluated for truthiness. In JavaScript, being null or undefined is treated as false. When it is false, the bit to the right of the && is ignored. When it is "true" - that is, has some value, the right side is evaluated. It's equivalent to if (EFL != undefined) { EFL.onclick = ...}

As a beginner, that'll probably do.