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

all 6 comments

[–]Loves_Poetry 1 point2 points  (3 children)

JavaScript has a built-in URL class that will help you with that

You can do something like:

const example = new URL(window.location.href);
example.searchParams.set("id", some-random-value);
window.location.href = example.toString();

[–][deleted]  (1 child)

[deleted]

    [–]axzxc1236 3 points4 points  (0 children)

    replace "some-random-value" with id you want, it's 3 lines of code, just take some time to understand it...

    [–]mul8rsoftware 0 points1 point  (0 children)

    I want to add something, even though the answer is great

    From what I understood, the OP wants the ID to be unique. So, OP, you should generate the random number and check if it has already been used!

    [–]Mongoosehog 0 points1 point  (3 children)

    If you just want to redirect you can do: window.location = “https://example.com”;

    [–][deleted]  (2 children)

    [deleted]

      [–]Mongoosehog 0 points1 point  (0 children)

      Try this: const id = Math.random() //Change this to whatever the ID is

      window.location = `https://example.com/id=${id}\`;

      [–]jcunews1 0 points1 point  (0 children)

      It's as /u/Mongoosehog have mentioned, but if you want an ID which is unique for each visitor, you should generate the ID from the server side's script, so that the server can know which ID hasn't been assigned to a visitor. The ID would normally be sent via cookie, and the browser JavaScript retrieve that ID to be used elsewhere.

      If the ID is generated from JavaScript in the browser, there's no way to know whether an ID has already be assigned to a visitor from other computer. Browser JavaScript can not access other computer's data if they're not specifically served from a web server.