In the actual development, some through get to the background to exchange data, need to use the data in the URL, so we need to get to the URL useful information, the following encapsulated function can be the URL to parse the very thorough, can be used directly:
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;//len = 2alert (a.search) 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 ('/')) }; }
The function uses the following:
var myurl = parseURL (window.location.href);//The URL of the current page is parsed by the parseURL function; Window.location.href can be replaced with any URL to parse, if you write the other URL directly, the format should be string;
var search_obj = Myurl.params; The parsing method is to parse the contents of search into objects, which makes it easy to make data calls;
var url_post = Myurl.post; The port number of the current page;
URL parsing function Encapsulation of JS