URL related in JS

Source: Internet
Author: User

First, look at a function to parse the URL, the general format of the URL is Http://domain.com/?search=a#hash

functionparseURL(URL){var a= document.CreateElement(A); /Create an A element a. href= URL; A's link is URLReturn{Source: URL, protocol: A. protocol.Replace(‘:‘,‘‘),//protocol, change colon to space host: A. hostname,//Host name Port: A. Port,//Port query: A. Search,//question mark and params after the question mark:(function(){VAR ret={}, SEG= A. Search.Replace(/^\?/,‘‘).Split(' & '),,//change the question mark inside query to a space and return as an array, separated by &. In the following example, get id=225 M=hello len= SEG. length+ N=0, S;For(I; I<len; I++){If(!seg[I]){Continue;}//no seach ignores s= SEG[I].Split(=);//Otherwise separated by an equal sign RET[s[0]]= S[1]; id=225 M=hellow}return ret; return to the above request})(), file:(A. pathname.Match(/\/([^\/?#]+) $/i)||[,‘‘])[1],//pathname is the path name, after port, search front, z regular expression is a bit messy, head big, and then analyze the hash: A. hash.Replace(‘#‘,‘‘),//#top变为top path: A. pathname.Replace(/^([^\/])/,'/$1 '),//Continue disorderly relative:(A. href.Match(/tps?:\ /\/[^\/]+(.+)/)||[, "]) [1) . Replace (/^\//, ") . Split ( '/' };} 

/span>
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.segments.port//= ' 8080 ' myurl.protocol //= ' http ' myurl.source //= ' http://abc.com:8080/dir/index.html?id=255&m=hello#top '      
< Span class= "token regex" >          

JS in URL-related

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.