Get GET value from my javascript URL

0
/index.php/view/corte/8

I need to get that 8 out of the equation but how can I do with Javascript?

    
asked by DoubleM 25.05.2018 в 23:33
source

4 answers

3

To get the URL and the last value of it you can use the split method as follows:

function getUrl(){
     //Se obtiene el valor de la URL desde el navegador
     var actual = window.location+'';
     //Se realiza la división de la URL
     var split = actual.split("/");
     //Se obtiene el ultimo valor de la URL
     var id = split[split.length-1];
     console.log(id);
}
    
answered by 25.05.2018 / 23:59
source
0

You can achieve this by using the Location object that is part of the global object window and doing a little manipulation of strings:

let paths = window.location.pathname.split('/');
let id = paths[paths.length-1];
    
answered by 25.05.2018 в 23:52
0

Using javascript, you can use the following code " assuming the last position of the URL is the value you want to get ":

// Esta es la url que pones de ejemplo:
var mi_url = "/index.php/view/corte/8";

// En este arreglo guardaremos la URL "separada por la barra diagonal":
var arreglo_datos = mi_url.split('/');

// En una variable separada almacenamos el valor que se encuentra al final de la URL.
var valor_a_obtener = arreglo_datos[arreglo_datos.length - 1];

// Imprimimos el valor obtenido "tanto en el control de tipo (span) como en la consola.
document.getElementById('spn_resultado').innerHTML = valor_a_obtener;
console.log(valor_a_obtener);
El resultado es: <span id="spn_resultado" />
    
answered by 25.05.2018 в 23:55
-1
String url = "/index.php/view/corte/8";

String[] valores = url.split("/");

If the URL is always the same, the code works, if you change the position but it is only a number, you would only validate that because the others are letters.

    
answered by 25.05.2018 в 23:42