Generally, we use Javascript to process and parse URLs using location objects. In today's code tips, we use another heterogeneous method to process and parse URLs.
The Code is as follows:
Function parseURL (url) {var a = document. createElement ('A');. href = url; return {source: url, protocol:. protocol. replace (':', ''), host:. hostname, port:. port, query:. search, params :( function () {var ret ={}, seg =. search. replace (/^ \? /, ''). Split ('&'), len = seg. length, I = 0, s; for (; I
How to use this method? Simple:
var myURL = 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'
GBdebug online debugging unique address: http://www.gbtags.com/gb/debug/c8946680-fb7f-4a2b-8cb7-9c81b420a74a.htm
The running result is as follows:
Read the original article: Processing and parsing URLs with different types of Javascript