URL parsing function Encapsulation of JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.