2010-05-05 17 views

उत्तर

4

अपने URL एक चर में आयोजित किया जाता है, तो आप विभाजन() विधि निम्न करने के लिए उपयोग कर सकते हैं:

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'; 

आप पार्स करने के लिए चाहते हैं मैं की तरह उत्पादन होना चाहता हूँ दस्तावेज़ के वर्तमान URL, आप 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

धन्यवाद .... यह काम करता है ... । –

0

कोड

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

var list = s.split("/") 

console.log(list); 

आउटपुट

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

तो आप निम्नलिखित विशेषताओं का उपयोग कर सकते हैं:

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

संदर्भ: https://gist.github.com/jlong/2428561

संबंधित मुद्दे