javascript - Remove string from URL when site is loading
I want to remove specific or full parameters from the URL of the website when it is actively loading in the browser. I want to do this because some website including additional strings.
https://www.example.com/?gclid=anything
https://www.example.com/?fbclid=anything
https://www.example.com/?msclid=anything
These are the tokens sent by third party like Google, Facebook, etc. I want to remove that.
For example, if peoples click my link on facebook https://www.example.com/
than Facebook will include https://www.example.com/?fbclid=something
but i want ?fbclid=something
should be removed and peoples land to https://www.example.com/
instead of https://www.example.com/?fbclid=something
My code :
$url = strtok($_SERVER["REQUEST_URI"], '?');
I have already checked Strip off URL parameter with PHP & How to remove the querystring and get only the URL? but no success.
Please suggest me how to achieve this using PHP or JavaScript.
Answer
Solution:
What you're looking for is history.pushState
https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
The below example will change the url https://www.example.com/?fbclid=something
to https://www.example.com/
window.history.pushState({}, '', '/');
Answer
Solution:
For Reference :
Answer by @dvicemuse & @Techno
<script>
//window.history.pushState('', '', '/');
//window.history.pushState({}, '', '/');
window.history.pushState({}, '', window.location.pathname);
</script>
Documents :
- https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
- https://dmitripavlutin.com/parse-url-javascript/#5-pathname