0

I have a link with a variable? Sid = .... being executed, but I want that if this variable exists it does not perform the function.

get the value of the variable and if it is false execute.

URL: https://example.com/?sid=2134912fhjkdhfsjdhf234829347wdjfsdf

function oldPrice() {
  var url = window.location.search.replace("?", "");
  var items = url.split("");
  var array = {
    'id': items[20]
  }
  var result = array.id;

  if (window.location.href != "https://www.mysite.com.br/" || result == false) {
    const spans = document.querySelectorAll(".old-price.sly-old-price.no-display");
    for (let i = 0; i < spans.length; i++) {
      const span = spans[i];
      span.classList.add("forceview");
    }
  }
  return null;
}
oldPrice();
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Richard Nicson
  • 137
  • 1
  • 7
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – mrid Aug 29 '19 at 12:18

2 Answers2

1

Use URL and URLSearchParams - like this

When it does not exist, the test is null

const url = new URL("https://example.com/?nosid=xxxx")
const searchParms = new URLSearchParams(url.search); // OR location.search for the page url params

var sid = searchParms.get('sid') ? true:false;
console.log(sid)

if (window.location.href !== "https://www.mysite.com.br/" || !sid) {
  console.log("executing")
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • From what I saw he finds the value of sid but no if is always returning false... if(sid == false){true}else{false} – Richard Nicson Aug 29 '19 at 12:42
  • I do not understand what you mean. Do you have `sid=true` or `sid=false` in the query string? Then please show proper examples in your question – mplungjan Aug 29 '19 at 12:52
  • sid=false. I need to be true as it finds the value. – Richard Nicson Aug 29 '19 at 12:55
  • Wouldn't it be better to just check because the query answers false even if there is value? – Richard Nicson Aug 29 '19 at 13:20
  • I have absolutely no idea what you mean. Please try to explain what you mean by true and false in this case. My code will return false if there is NO sid or NO value in the sid is that what you mean? Don't you need to return false when sid=nothing? – mplungjan Aug 29 '19 at 13:22
  • sorry, if it finds "nosid" returns true. if (nosid == false) {true} else {false} – Richard Nicson Aug 29 '19 at 13:25
  • On my system, itself finding value returns false. – Richard Nicson Aug 29 '19 at 13:26
  • You are confusing me. Please update your question with example query strings and what you expect them to do. I use nosid to a) show it is NOT sid and 2) to break code using indexOf or includes – mplungjan Aug 29 '19 at 13:27
  • var sid = "12314"; if(sid != false){true}else{false} response:true – Richard Nicson Aug 29 '19 at 13:38
  • That statement makes no sense to me. What var. You are asking about URL parameter. != will cast a falsy value to false and a truthy value to true – mplungjan Aug 29 '19 at 13:38
0

You can use the following code :

var url = "https://example.com/?sid=2134912fhjkdhfsjdhf234829347wdjfsdf";
if(url.includes("sid")) { console.log("true") }
mplungjan
  • 169,008
  • 28
  • 173
  • 236