0

I've Frankensteined the following script to allow a hidden form field to be populated by a value that has been defined in the URL. This script works, but the problem is that anything following "product=" gets shown, so things like the utm_ parameters as well.

So now I'm trying to figure out a way to get the script to only work for text following "?product=" and/or "&product=" up until the next "&".

var url = location.search;
var product = url.substring(url.indexOf('product=')+8, url.length);
var productStripped = product.replace("%20", " ");
$("#product").val(productStripped);

How can I best achieve this?

Thanks!

Bolteh
  • 57
  • 5

1 Answers1

1

Either extend Jquery with https://github.com/allmarkedup/purl or Use a function as described here: https://stackoverflow.com/a/7732379/2970095

OR

The Library purl is no longer actively maintained. Purl's author recommends URI.js as an alternative. https://medialize.github.io/URI.js/

NeilWkz
  • 245
  • 1
  • 8
  • Ah! Thanks! I'm such a novice that I don't even know the correct terminology, so I had a hard time finding exactly what I needed. – Bolteh Apr 24 '17 at 14:29