i need to get the language that the user is using based on the document.location
urls are of the type:
domain.com/en/blabla.html
domain.com/es/blabla.html
domain.com/it/blabla.html
so i was trying like this:
function getLan(){
var idioma = document.location;
var idiomaTmp = idioma.split("/");
return = idiomaTmp[1];
}
but (i don’t understand but) i get this error at firebug
idioma.split is not a function
[Detener en este error] var idiomaTmp = idioma.split("/");
any idea why? or maybe a better solution?
document.locationis an Object. Although is does have a custom toString method, so thatalert(document.location)shows the actual url, it itself is not a String, and String methods don’t work on it. What you want is to convert this Object to a String before using String methods:As for a better solution, try regular expressions: