Streaming live at 10am (PST)

Why is pathname.split array length the same on all pages?

Hi, i am creating a button to perform different functions depending on if it is in a subfolder or not.
I am splitting the pathname of whatever page i am on by ‘/’ and running a different function if the pathname.split(’/’).length is: [1], [2], or [3].

The problem is that on all the pages on my site whether they are:
mysite.com/
mysite.com/page
mysite.com/folder/page
mysite.com/folder/folder/page
are all giving me an array length of [2], which is preventing me from running an if statement for each length.

var host = window.location.host;
var path = window.location.pathname;
var sub = path.split('/');
var subL = sub.length;
alert(subL);

does anyone have any insight on this? Thanks

I’m guessing you either have another logic error e.g. location is not what you are expecting, or you are expecting different values for mysite.com/ vs mysite.com/page.

so i managed to work it out. for some reason it just wasn’t defining the variable properly.
instead of going through path > sub > subL as i did in the original code i posted, i just defined it straight up:

var subL = window.location.pathname.split('/').length;

not sure why it didn’t work the other way. thanks for your reply :+1:t2: