Webpage: https://www.ualc.org/growthworkshops-search
I am using the wixLocation.path[1] as an input to a filter. For example (domain.org/path1/path2)… Whereas “path2” sets the $w ( “#selectiontag” ). value to equal “path2”
The filter and wixLocation function work great unless “path2” is no longer defined…
The problem is when you remove /path2, it breaks the code because “wixLocation.path[1]” becomes undefined.
Here’s the code I’ve tried:
$w.onReady(function () {
});
import wixLocation from 'wix-location';
import wixData from 'wix-data';
let path
if (wixLocation.path[1].length !== 0) {
path = wixLocation.path[1].replace("-", " ")
}
else {
path = ""
}
console.log(path);
How do I get the page to check if “/path2” is defined, and if not to move on to set the value to “”
Are you importing Wixlocation? Never used this path before, but why are you indexing into the path? why not wixLocation.path.length !=?
Thanks for a quick response Kyle! Because of the API for wixLocation, when you pull the path out you get the path and the prefix. I need to index into the path because I only want the “path” not the “prefix.” I want to run this conditional statement based on the absence of the “path.” However, when the path is added, I want the conditional statement to set the value.
Here’s the API info:
"Premium sites:
https://www.domain.com/myPrefix/myPath
https://domain.com/mammals/elephant
import wixLocation from 'wix-location';
// ...
let path = wixLocation.path; // ["mammals", "elephant"]
When “myPath” is present, I want to set the value based on the wixLocation input.
When “myPath” is not present, I want to set the value to “”
I’m just not sure how to create a conditional statement based on something that’s not there… Trying to figure out a different way to do it.
HI - hopefully I’m understanding in that you want to basically check when path is “undefined”? Or basically holds no value.
In Javascript you can check for an undefined value
let a = 3
console . log ( typeof a ) → number
Now, remove let a =3 and typeof a comes back as undefined. If you had the following
let a
if ( a === “” ) {
console . log ( “Aaaaa” )
}
if ( typeof a === ‘undefined’ ) {
console . log ( “undefined var” )
}
Then the second IF would work as it is undefined. It does not hold a “” . When you try to get the length of an undefined variable you are seeing that error. You can also use:
if ( ! a ) {
console . log ( “Ooops!” )
}
Maybe something like the below. Thanks for the intro to the path as never used it. Thought it just returned a string, hence why I wasn’t sure why you were indexing.
if (typeof wixLocation.path === 'undefined') {
if(!wixLocation.path[1]){path=wixLocation.path[1].replace("-"," ")}
Kyle, that did it! Such a simple shift in my thinking that I was just completely blanking on. Thank you so much for the help.
Here was the final code snippet to define “path” only if the “path” exists in the URL.
$w.onReady(function () {
});
import wixLocation from 'wix-location';
import wixData from 'wix-data';
let path
//if the path is undefined, let path value be ""
if (wixLocation.path[1] === undefined ) {
path = "";
console.log("no path")
}
//if the path is defined, define path
else {
path = wixLocation.path[1].replace("-", " ");
console.log("path defined")
}
console.log(path);
@communications19033 all good and pleased it is working