lundi 19 février 2018

IF/Else statement giving me wrong number

I'm trying to set the variable "productid" to a product number in the URL and set it to another variable "productID". The if statement works. The else statement, which is my fallback product number in case there is no product number in the url, gives me this: tp://localhost:63342/IntegralWebsite/index.php.

Here's my code:

function prod() {
    let productid;
      if (url.substring(url.lastIndexOf("ID=") + 3).split("#")[0]) {
          productid = url.substring(url.lastIndexOf("ID=") + 3).split("#")[0]; //Get product ID from url
          alert(productid);
          // localStorage.setItem('productid', productid);
      } else {
        productid = 167160840221;
          // localStorage.setItem('productid', productid);
          alert(productid);
      }
    return productid;
  }

  let productID = prod();
  alert(productID);

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire