0

I have a function in JavaScript which returns square of the given number. To get output I take input from user in a form field and on clicking a button trigger that function.

I want to remove that typing parameter and button clicking thing. Is there a way I can have a link which on clicked will give me square of say 100. I want to have that link dynamic. Example, square.com/100 should give me square of 100. Square.com/2 should give square of 2. But this is to be done on dynamic hosting like GitHub pages.

frameworks: I am not using any framework but I will host it on github pages so any framework that will work with github pages that is static hosting is also good

As we are not using any backend processing I think this can be done. But not sure how

1 Answers1

0

You can use query parameters in a url (e.g. https://example.org/?number=1234) and then parse it client side. One way to do that is:

const params = new URLSearchParams(document.location.search);
const myNumber = Number(params.get('number'))

For more ways to parse the query string see How can I get query string values in JavaScript?

A_A
  • 1,832
  • 2
  • 11
  • 17