I have a URL that may or may not contain parameters in it. Ex: www.site.com.br/?id=1
or www.site.com.br
.
I need to check if there is any parameter in this URL and with that I can add one more at the end. For example:
- If URL is
www.site.com.br
itwww.site.com.br/?id=1
- If URL is
www.site.com.br/?utm=teste
itwww.site.com.br/?utm=teste&id=1
That is, the URL may or may not have a parameter and this parameter may be random, so I can not know exactly what parameter I'll have.
I need to know this, because I need to add a parameter id=SC
to the end of the URL and sometimes it is giving error, because a previous parameter already exists. Here's how I'm doing:
//Pego a url anterior que o usuário estava e redireciono para a mesma url porém adicionando um parametro no final
window.location.href = document.referrer+'?id=CS';
With the above code, if the previous URL is www.site.com.br/?utm=teste
the new URL will be site.com.br/?utm=teste?id=CS
and the user will not be able to access the page.