title: Split URL to set cookie using Javascript
body: <p>Hoping you can help. I think I'm nearly there but my code has a slight error in it. I'm trying to set a cookie which sets the cookie value based on a section of the page url.</p>
  <p>For example my cookie name is &quot;model&quot; and my cookie value is populated based on the url</p>
  <p><a href="https://www.website.co.uk/cars/100sr/" rel="nofollow noreferrer">https://www.website.co.uk/cars/100sr/</a></p>
  <p>in the example above the cookie value should be set as 100sr</p>
  <p>However, I've just noticed an error, where if the customer visits my website with a query string on the url, it's setting the cookie value to the query string content not the 100sr</p>
  <p>e.g.</p>
  <p><a href="https://www.website.co.uk/cars/100sr/?testing" rel="nofollow noreferrer">https://www.website.co.uk/cars/100sr/?testing</a></p>
  <p>the url above using my current code would set the cookie as ?testing when I want it to still be set as 100sr. I'm guessing my code is taking the content after the LAST / is there perhaps a way to specify to take the content after the 2nd / instead?</p>
  <p>Code below</p>
  <pre><code>&lt;script&gt;$(&quot;#carbtn&quot;).bind(&quot;click&quot;, function() {
    const strings = window.location.href.split(&quot;/&quot;).filter(str =&gt; !!str);
    document.cookie=`model=${strings[strings.length - 1]};path=/;`
  });&lt;/script&gt;
  </code></pre>