Uncaught TypeError: Cannot read property 'split' of undefined window location

I have this error and I google about it. But I didn´t found my issue

  var url = window.location.hash.split('?Token=')[1];
        url = url.split('Token=');

        function cambiarContrasena(usuario, completado, fallo) {
            apiService.post('/api/usuario/cambiarContrasena?token='+url, usuario,
            completado,
            fallo);
        }

My value isn´t empty or undefinied, can anyone help me there? Thanks!


Solution 1:

Use href instead of hash.

 var url = window.location.href.split('?Token=')[1];
 // url = url.split('Token='); no need to split it again.

 function cambiarContrasena(usuario, completado, fallo) {
        apiService.post('/api/usuario/cambiarContrasena?token='+url, usuario,
        completado,
        fallo);
 }

Difference between hash and href is they return different part of URL. Here is the anatomy of URL:

Anatomy of a URL
(source: doepud.co.uk)

In this case window.location.hash returns #00h02m30s where windows.location.href returns entire URL.