Javascript URL redirect code


i need to develop some javascript code that is not working.

My need is:

When the user accesses a certain page, I would like to supplement the URL with some parameters, for example:

If the domain is, redirect it to

Remembering that it can also go into a subdirectory, for example:, you should also add

Basically, if the domain matches, it adds a variable at the end.

Important detail, I did this and it was looped, because when redirecting it identifies the domain in the same way, therefore, if the domain matches, there must be another check to see if the parameter does not already exist in the URL.


Adding only &variavel=1 to the URL seems wrong to me, in which case I should use the "query" string that starts with ? . In other words, using .

So you can have the following script on every page that needs this functionality: = || '?variavel=1';

Combining this with location.hostname can verify that the domain is correct as location.hostname gives exactly this information.

So your full code could be:

if (location.hostname == '') = || '?variavel=1';
Scroll to Top