lets say that we are studding a url string:
http:/domain.com/en/#the-file.html/section-4
where i have codified:
http:/domain.com/en/#{html file}/{id of the div}-{level}
this way i can know by
var info = window.location.hash;
var temp = info.split("/");
var url = temp[0];
url = str_replace('#','',url);
var temp1 = temp[1];
temp1 = temp1.split('-');
var seccion = temp1[0];
var nivel = temp1[1];
where
console.log('url: '+url);
console.log('info: '+nivel);
console.log('seccion: '+seccion);
provide me those codified vars
the problem is when, for example, the url is
http://domain.com/es/
that firebug jumps this error:
temp1 is undefined
Any idea how to prevent it?
Instead of using
split, I recommend to use a RegExp, which does not break when multiple slashes or hyphens show up.The code below will always define the variables
url,nivelandseccion. If the hash is not existent, these variables will be an empty string.