2010-09-16 14 views
18

Ho un URL come questo:ottenere l'ID di URL con jQuery

http://www.site.com/234234234 

ho bisogno di afferrare il Id dopo /, quindi in questo caso 234234234

Come posso fare questo facilmente?

risposta

35

Prendi un substring dopo la last index of/.

var url = 'http://www.site.com/234234234'; 
var id = url.substring(url.lastIndexOf('/') + 1); 
alert(id); // 234234234 

È solo JavaScript di base, nessun jQuery coinvolto.

+2

Funziona alla grande! Ma cosa succede se c'è url-roba sul lato destro dell'URL? – Tom

5
var url = "http://www.site.com/234234234" 
var stuff = url.split('/'); 
var id = stuff[stuff.length-1]; 
//id = 234234234 
1

Utilizzando il jQuery URL Parser plugin, si dovrebbe essere in grado di fare questo:

jQuery.url.segment(1) 
+0

Mi piace questo perché molte altre soluzioni proposte sembrano fallire quando ci sono ancore, parametri di query, ... coinvolti. –

29
var url = window.location.pathname; 
var id = url.substring(url.lastIndexOf('/') + 1); 
7
var full_url = document.URL; // Get current url 
var url_array = full_url.split('/') // Split the string into an array with/as separator 
var last_segment = url_array[url_array.length-1]; // Get the last part of the array (-1) 
alert(last_segment); // Alert last segment 
+0

Grazie. Punto e virgola mancanti in 2a linea – Elnoor

1

Proprio perché posso:

function pathName(url, a) { 
    return (a = document.createElement('a'), a.href = url, a.pathname); //optionally, remove leading '/' 
} 

pathName("http://www.site.com/234234234") -> "/234234234" 
1

provare questo javascript

Snippet per ottenere il paramete rs dall'URL. Utilizzare javascript per ottenere i parametri URL dalla posizione della finestra corrente o dall'URL statico nell'argomento della chiamata alla funzione.

javascript

function getUrlParameters(parameter, staticURL, decode){ 

     var currLocation = (staticURL.length)? staticURL : window.location.search, 
      parArr = currLocation.split("?")[1].split("&"), 
      returnBool = true; 

     for(var i = 0; i < parArr.length; i++){ 
      parr = parArr[i].split("="); 
      if(parr[0] == parameter){ 
       return (decode) ? decodeURIComponent(parr[1]) : parr[1]; 
       returnBool = true; 
      }else{ 
       returnBool = false;    
      } 
     } 

     if(!returnBool) return false; 
    } 

Per ottenere il parametro “id” dall'alto URL statico, utilizzare il seguente:

var idParameter = getUrlParameters("id", "http://www.example.com?id=1234&auth=true", true); 

o

var idParameter = getUrlParameters("id", "", true); 
+1

Molto utile .. Grazie mille – Zohaib

1

mio URL come questo http://www.default-search.net/?sid=503. Voglio ottenere il 503. Ho scritto il seguente codice.

var baseUrl = (window.location).href; // You can also use document.URL 
var koopId = baseUrl.substring(baseUrl.lastIndexOf('=') + 1); 
alert(koopId)//503 

Se si utilizza

var v = window.location.pathname; 
console.log(v) 

otterrete solo "/";

+0

questo non ha funzionato per me, ma il post di Rohrbs sopra funzionava alla grande. – tbone