Code:
functionparseurl (URL) {varA = document.createelement (' A '); A.href=URL; return{source:url, Protocol:a.protocol.replace (‘:‘,‘‘), Host:a.hostname, Port:a.port, Query:a.search, params: (function(){ varRET ={}, seg= A.search.replace (/^\?/, '). Split (' & ')), Len= seg.length, i = 0, S; for(; i<len;i++) { if(!seg[i]) {Continue; } S= Seg[i].split (' = ')); ret[s[0]] = s[1]; } returnret; }) (), File: (A.pathname.match (/\/([^\/?#]+) $/i) | | [,‘‘]) [1], Hash:a.hash.replace (‘#‘,‘‘), Path:a.pathname.replace (/^ ([^\/])/, '/$1 '), Relative: (A.href.match (/tps?:\ /\/[^\/]+(.+)/) || [,‘‘]) [1], Segments:a.pathname.replace (/^\//, '). Split ('/')) }; }
Use:
varMyurl = parseURL (' http://abc.com:8080/dir/index.html?id=255&m=hello#top ')); Myurl.file; //= ' index.html 'Myurl.hash;//= ' top 'Myurl.host;//= ' abc.com 'Myurl.query;//= '? Id=255&m=hello 'Myurl.params;//= Object = {id:255, M:hello}Myurl.path;//= '/dir/index.html 'myurl.segments;//= Array = [' dir ', ' index.html ']Myurl.port;//= ' 8080 'Myurl.protocol;//= ' http 'Myurl.source;//= ' Http://abc.com:8080/dir/index.html?id=255&m=hello#top '
[Go] how heterogeneous javascript handles and resolves URLs