JavaScript parseurl function (get URL parameters from abroad) _javascript tips

Source: Internet
Author: User
Tags hash
function code
Copy Code code as follows:

function parseURL (URL) {
var a = document.createelement (' a ');
A.href = URL;
return {
Source:url,
Protocol:a.protocol.replace (': ', '),
Host:a.hostname,
Port:a.port,
Query:a.search,
Params: (function () {
var ret = {},
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];
}
return ret;
})(),
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 ('/')
};
}

Usage:
Copy Code code as follows:

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 '

Demo Code:
<script type= "Text/javascript" > Function parseurl (URL) {var a = document.createelement (' a '); A.href = URL; return {source:url, protocol:a.protocol.replace (': ', '), Host:a.hostname, Port:a.port, Query:a.search, params: (function () {var ret = {}, 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]; return ret; }) (), File: (A.pathname.match (/\/([^\/?#]+) $/i) | | [,'']) [1], Hash:a.hash.replace (' # ', '), Path:a.pathname.replace (/^ ([^\/])/, '/$1 '), Relative: (A.href.match /\/[^\/]+(.+)/) || [,'']) [1], Segments:a.pathname.replace (/^\//, '). Split ('/')}; var myurl = parseURL (' http://abc.com:8080/dir/index.html?id=255&m=hello#top '); alert (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 ' </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

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.