title: Remove Affiliate Query String from Url Javascript
body: <p>In my shopping cart I create affiliate urls like so:</p>
  
  <pre><code>http://mysite.local/phones-and-pdas/iphone?z=5509d173cffeb
  </code></pre>
  
  <p>I'd like to use history.pushState to remove the query string starting at the ?.</p>
  
  <p>I've tried using <code>slice()</code> and <code>split()</code> but this seems to effect other urls as well, even when they don't contain <code>?z=</code></p>
  
  <pre><code>function trackingLink() {
      var href = window.location.href;
      var url  = href.slice(0, href.indexOf('?z='));
  
      history.pushState(null, null, url);
  }
  </code></pre>
  
  <p>For example when I go to:</p>
  
  <pre><code>http://mysite.local/account/dashboard
  </code></pre>
  
  <p>The address url is changed to:</p>
  
  <pre><code>http://mysite.local/account/dashboar
  </code></pre>
  
  <p>As a note, the above code does work perfectly on the affiliate link.</p>
  
  <p>I'm sure this is a simple tweak but I couldn't find a specific answer when searching.</p>