2010-05-05 30 views

Respuesta

4

Si la URL se lleva a cabo en una variable, puede utilizar el método split() para hacer lo siguiente:

var url = 'http://mywebsite.com/folder1/folder2/index'; 
var path = url.split('/'); 

// path[0]  === 'http:'; 
// path[2]  === 'mywebsite.com'; 
// path[3]  === 'folder1'; 
// path[4]  === 'folder2'; 
// path[5]  === 'index'; 

Si desea analizar la URL actual del documento, se puede trabajar en window.location:

var path = window.location.pathname.split('/'); 

// window.location.protocol === 'http:' 
// window.location.host  === 'mywebsite.com' 
// path[1]     === 'folder1'; 
// path[2]     === 'folder2'; 
// path[3]     === 'index'; 
+0

Gracias .... funciona ... . –

0

Código

var s = "http://mywebsite.com/folder1/folder2/index"; 

var list = s.split("/") 

console.log(list); 

salida

["http:", "", "mywebsite.com", "folder1", "folder2", "index"] 
3
var reader = document.createElement('a'); 
reader.href = "http://test.example.com:80/pathname/?query=param#hashtag"; 

continuación, puede utilizar los siguientes atributos:

reader.protocol 
reader.hostname 
reader.port 
reader.pathname 
reader.search 
reader.hash 
reader.host; 

Referencia: https://gist.github.com/jlong/2428561

Cuestiones relacionadas