shortysbest Posted December 13, 2011 Share Posted December 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253089-javascript-split-returns-undefined-is-there-a-way-to-return-false-if-not-true/ Share on other sites More sharing options...
marcelobm Posted December 13, 2011 Share Posted December 13, 2011 var uri = hash_value.split("/"); var page = uri[1] ? uri[1] : ''; var user_id = uri[2] ? uri[2] : ''; var page2 = uri[3] ? uri[3] : ''; Quote Link to comment https://forums.phpfreaks.com/topic/253089-javascript-split-returns-undefined-is-there-a-way-to-return-false-if-not-true/#findComment-1297494 Share on other sites More sharing options...
shortysbest Posted December 13, 2011 Author Share Posted December 13, 2011 Ahhh I completely forgot about that, I have used that in the past. Been on break from coding haha, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/253089-javascript-split-returns-undefined-is-there-a-way-to-return-false-if-not-true/#findComment-1297495 Share on other sites More sharing options...
marcelobm Posted December 13, 2011 Share Posted December 13, 2011 you're welcome Quote Link to comment https://forums.phpfreaks.com/topic/253089-javascript-split-returns-undefined-is-there-a-way-to-return-false-if-not-true/#findComment-1297496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.