Home > other >  Basic website that simply shows query string
Basic website that simply shows query string

Time:04-08

I am trying to do the following: We need an easy way for user to find out what "id" their system has for remote support for example. I want to create a shortcut on everyone's desktop that leads to a website which then shows them their id with a query string (?id=..).

For example: the shortcut to https://example.com/support/index.html?id=80085 should open up a website which then shows "Your ID is 80085, please contact 1 123 456 for support or visit our online help page"

I am able to do all of that in html except showing the id in the body of the website

Does anyone have an example how I could do something like this?

CodePudding user response:

If I understand what you are trying to do, basically I would create an HTML page with a simple div with an ID (for it to be found in script), and create a script that manipulates the url and places the corresponding text in the page.

...
<body>
  <div id="main-div"></div>
  <script>
    const div = document.getElementById('main-div');
    const element = document.createElement('p');
    const id = new URLSearchParams(window.location.search).get('id');
    if (!id) {
      const emptyIdText = document.createTextNode('There was an error');
      element.appendChild(emptyIdText);
    } else {
      const idText = document.createTextNode('Your ID is '   id   ', please contact  1 123 456 for support or visit our online help page');
      element.appendChild(idText);
    }
    div.appendChild(element);
  </script>
</body>
...

If you need, you can place this script in a .js file and import it via the src attribute of the script tag. Hope this helps!

CodePudding user response:

You can get the id parameter by converting the current url to a URL object in JavaScript like so

// get the current url
const url = new URL(window.location.href)

// extract the id parameter from the url
const id = url.searchParams.get('id')

// print the results to the screen
console.log(`Your ID is ${id} please call ...`)

  • Related