1

The follwing javascript code is doing what it's intended to do, which is to replace the URL but then the page won't stop reloading/refreshing. Is there a way to stop it?

Original page is: www.xyz.com/used-vehicles, but I want to automatically use the checkbox filter options on that page which adds to the URL after used-vehicles ?search=certified%2Cpreowned

<script>
document.location.replace('/used-vehicles?search=certified%2Cpreowned');

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Adeel
  • 13
  • 2
  • 2
    Possible duplicate of [Modify the URL without reloading the page](https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) – gaetanoM Aug 16 '18 at 16:19
  • Put this under condition to match if URL already contains the replaced string. – Abhishek Aug 16 '18 at 16:27

1 Answers1

0

Check if your url already doesn't contain search=certified%2Cpreowned:

if(window.location.href.indexOf("search=certified%2Cpreowned") == -1){
   window.location.replace('/used-vehicles?search=certified%2Cpreowned');
}
AmmoPT
  • 958
  • 1
  • 9
  • 16
  • Follow up question: This method wouldn't create two sessions in google analytics, would it? – Adeel Aug 16 '18 at 16:36
  • Most likely wouldn't create two sessions, but it might create two page hits though. I couldn't find anything conclusive and light enough that I could point you to, so I would test it myself to be a 100% sure. – AmmoPT Aug 16 '18 at 17:03