/index.php/view/corte/8
I need to get that 8
out of the equation but how can I do with Javascript?
/index.php/view/corte/8
I need to get that 8
out of the equation but how can I do with Javascript?
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);
}
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];
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" />
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.