Can you change the value of a querystring parameter in javascript?

Asked

Viewed 759 times

1

For example I have this querystring www.meusite.com.br?parametro=1. I wanted to exchange the value 1 for another value when a given function is called, without having to load the page again (if possible).

  • you already have mounted this querystring in javascript ? if you have post please

  • I don’t have no, I’m using Asp.net c# and redirect it through it

  • It is easier to pass via POST or store cookies than to use the query

  • is too vague.. www.meusite.com.br?parametro=1 comes from where? is the url bar address? is a string? ,anyway..

1 answer

2

To change the URL via javascript would be like this:

var Url = window.location.host;
document.location = Url + "?parametro=2"; //muda o valor de "parametro" para 2

However it is not possible to change the URL without re-loading the page, this is a security restriction.

Imagine that the user receives a fake email as if it were from a bank, clicks on a button and is sent to a fake page but soon after the URL is changed to the real address of the bank. The user fills in the login and via data ajax are recorded in a database and then redirects to a real page. It would be a serious security breach!

Although there are other ways to detect if the URL is false, most users do not know how to do it.

  • Filipe a doubt, and if for example, I already have the parameter = 1, and need to change to 2.... it will change the value or add a new parameter ?

  • It depends on what you do! In the example I gave changes the value because a concatenation is made with the URL.

  • So, but for example, assuming the url is www.meusite.com.br?parametro=1 and then I run your code, the url wouldn’t be www.meusite.com.br?parametro=1&parametro=2 ?

  • @Lucassousa no, it would be www.meusite.com.br? parametro=2, I made a change in the answer example, see if it now works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.