Using Javascript parsing links (URLs) is a common requirement, and this article describes a very sound, Javascript -written link (URL) parsing class that can accurately capture a complete The contents of each part of the url , including information such as the protocol, the user name and password contained in the URL, the hostname, port, pathname, parameters, Anchor Point (Fragment anchor), and so on.
<body> <div id= "Example" > <div id= "Example_main" > <script type= "Text/javascript" > If (typeof Poly9 = = ' undefined ') {var Poly9 = {}; } 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); } 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._field S[F]]; } poly9.urlparser.prototype._makegetter = function (field) {return function () {return this._values[ Field]; } var url = ' Http://user:password@www.jb51.net:1234/test/test.asp?id=1#test '; var p = new Poly9.urlparser (URL); document.write ("<strong>URL:</strong>" + URL + "<br><br>"); document.write ("Analytic result follows:<br><br>"); document.write ("<strong> Agreement:</strong>" + p.getprotocol () + "<br>"); document.write ("<strong> user:</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> path:</strong>" + p.getpathname () + "<br>"); document.write ("<strong> query string:</strong>" + p.getquerystring () + "<br>"); document.write ("<strong> anchor point:</strong>" + p.getfragment () + "<br>"); </script> </div> </div> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]