Jump to content

Javascript (split) returns undefined, is there a way to return false if not true


shortysbest

Recommended Posts

I am building a hash navigation system for my website, but this problem is something I find with everything in javascript.

For this particular problem what I'm doing is splitting the hash url to get different data from it, the problem is when there are levels of the url not there instead of returning nothing, it returns "undefined" which goes into the spot the real data should have gone had there been data present.

 

example URL scenarios:

www.mysite.com/#/home

www.mysite.com/#/user/2

www.mysite.com/#/user/2/info

www.mysite.com/#/user/2/photos/album

www.mysite.com/#/user/2/photos/album/922

 

and it could just keep going on.

 

Here is the code I am using at the moment:

var hash_value = event.value; //would be /user/2/info   (or whatever the URL might be as shown above)

var uri = hash_value.split("/");
var page = uri[1];
var user_id = uri[2];
var page2 = uri[3];

 

That code works perfect if "user" "2" and "info" are all there, however if it were to be: "/user/2"

var page2 would be "undefined", I need it to just return blank ""

 

 

Any way I could do this?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.