Javascript 連結(URL)解析類

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<script type="text/javascript"> /** * @projectDescription Poly9's polyvalent URLParser class * * @author Denis Laprise - denis@poly9.com - http://poly9.com * @version 0.1 * @namespace Poly9 * * See the unit test file for more examples. * URLParser is freely distributable under the terms of an MIT-style license. */ if (typeof Poly9 == 'undefined') { var Poly9 = {}; } /** * Creates an URLParser instance * * @classDescription Creates an URLParser instance * @return {Object} return an URLParser object * @param {String} url The url to parse * @constructor * @exception {String} Throws an exception if the specified url is invalid */ Poly9.URLParser = function(url) { this._fields = { 'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10 }; this._values = {}; this._regex = null; this.version = 0.1; this._regex = /^((w+)://)?((w+):?(w+)?@)?([^/?:]+):?(d+)?(/?[^?#]+)???([^#]+)?#?(w*)/; for(var f in this._fields) { this['get' + f] = this._makeGetter(f); } if (typeof url != 'undefined') { this._parse(url); } } /** * @method * @param {String} url The url to parse * @exception {String} Throws an exception if the specified url is invalid */ Poly9.URLParser.prototype.setURL = function(url) { this._parse(url); } Poly9.URLParser.prototype._initValues = function() { for(var f in this._fields) { this._values[f] = ''; } } Poly9.URLParser.prototype._parse = function(url) { this._initValues(); var r = this._regex.exec(url); if (!r) throw "DPURLParser::_parse -> Invalid URL"; for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined') { this._values[f] = r[this._fields[f]]; } } Poly9.URLParser.prototype._makeGetter = function(field) { return function() { return this._values[field]; } } </script>執行個體<script type="text/javascript"> var url = 'http://user:password@www.111cn.net:9901/pub/article.php?offset=10&perpage=10#fragment'; var p = new Poly9.URLParser(url); document.write("<strong>URL:</strong> " + url + "<br /><br />"); document.write("解析結果如下:<br /><br />"); document.write("<strong>Protocol:</strong> " + p.getProtocol() + "<br />"); document.write("<strong>Username:</strong> " + p.getUsername() + "<br />"); document.write("<strong>Password:</strong> " + p.getPassword() + "<br />"); document.write("<strong>Host:</strong> " + p.getHost() + "<br />"); document.write("<strong>Port:</strong> " + p.getPort() + "<br />"); document.write("<strong>Pathname:</strong> " + p.getPathname() + "<br />"); document.write("<strong>Query String:</strong> " + p.getQuerystring() + "<br />"); document.write("<strong>Fragment:</strong> " + p.getFragment() + "<br />"); </script></td> </tr></table>
提示:您可以先修改部分代碼再運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.