Question:
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 xxx.com.br/index.php, redirect it to xxx.com.br/index.php&variavel=1
Remembering that it can also go into a subdirectory, for example:
xxx.com.br/pasta/index.php, you should also add xxx.com.br/pasta/index.php&variavel=1
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.
Answer:
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 location.search
.
So you can have the following script on every page that needs this functionality:
location.search = location.search || '?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 == 'xxx.com.br') location.search = location.search || '?variavel=1';