I manage to solve the problem, and with the info that I provide, you guys could never have helped me. The problem was not in the code after all!
I have this habit of spending all my time looking for bugs in the code, and forgeting other issues like file location.
After some tests I found out that the function was not being called. The js file was in the same folder as my jsp, however, the jsp was called after a Servlet's forward(), so the URL in the browser bar was still from the first Servlet.
I spent days looking at the code, and the problem was solved after a glimpse on the browser bar...