Malasorte Malasorte - 3 months ago 15
Javascript Question

Paste variable value into URL query string

I get a variable from session storage like this:

var my_variable = sessionStorage.getItem("some_value");


Using Jquery or plain JS how can I paste the value of
my_variable
into the current document URL, inside a query string?

Example: If
my_variable
has the value "Hello_world", then the document URL should look like this:

www.example.com/some_page/index.html?q="Hello_world"

Answer

Without redirection it's possible with History API:

JS

history.replaceState(history.state, '{your-new-url}', '{your-title(optional)}');

Also check, if the browser support is enough corresponding to your requirements.

Read more here.

Comments