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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.