Heterogeneous Javascript processing and URL Parsing Methods

Source: Internet
Author: User


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

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.