This article mainly introduces JavaScript to the relative path to absolute path example, here is essentially two methods, by creating a DOM or through JavaScript computing, the need for friends can refer to the following
Here is essentially two methods, by creating a DOM or through JavaScript computing: 1 through the newly created image, the test will send a aborted request, and IE6 does not support the new Image changed to Document.createelement (' IMG ') is the same; the test should not like the scheme; code is as follows: function Getabsoluteurl (URL) { var img = new Image (); IMG.SRC = URL; //sets the relative path to image, at which point the request URL = img.src is sent; //at this point the relative path has become an absolute path IMG.SRC = null; Cancel request return URL; } getabsoluteurl ("Showroom/list"); 2 creates an anchor (link) that does not make any request (the request will be generated when the DOM is added), but IE6 does not support the following code:/*jslint regexp:true, White:true, maxerr:50, I Ndent:2/ function Parseuri (URL) { var m = String (URL). replace (/^s+|s+$/g, '). Match (/^ [^:/?#]+:)? ( //(?:[^:@]*(?::[^:@]*)?@)? (([^:/?#]*) (?::(d*))? ([^?#]*) (? [^#]*)? (#[ss]*)?/); //authority = '//' + user + ': ' + Pass ' @ ' + hostname + ': ' Port return (m? { href : m[0] | | ', PROTOCOL:M[1] | | ', AUTHORITY:M[2] | | ", HOST : m[3] | | ', HOSTNAME:M[4] | | ', port : m[5] | | ', PATHNAME:M[6] | | ', search : m[7] | | ', hash : m[8] | | ' }: null; } function Absolutizeuri (base, href) {//RFC 3986 function removedotsegments (input) { V AR output = []; Input.replace (/^.? (/|$)) +/, ') .replace (//( /|$) +/g, '/') .replace (///. $/, '/.. /') .replace (//?[ ^/]*/g, function (p) { if (p = = '/... ') { Output.pop (); } else { Output.push (p); &nbs P } }); return Output.join ("). Replace (/^//, Input.charat (0) = = '/'? '/' : ''); } href = parseuri (href | | ''); base = Parseuri (base|| ''); Return!href | | !base? Null: (Href.protocol | | base.protocol) + (Href.protocol | | href.authority? href.author ity:base.authority) + removedotsegments (Href.protocol | | href.authority | | href.pathna Me.charat (0) = = = '/'? Href.pathname: (href.pathname? (Base.authority &&!base.pathname? '/': ') + base.pathname.slice (0, base.pathname.lastIndexOf ('/') + 1) + Href.pathname): base.pathname) + (Href.protocol | | href.authority | | | href.pathname? href.search: (Href.search | | base.search)) + &nbs P href.hash; Because our products for the mobile Web page, has long been not supported IE6, the final use of the second option; This shows that the original ecological method to access all the image, anchor, the return of the absolute path, if you want to return to the original relative path, You can use the query Dom method, such as the Jquery.attr () method: Code as follows://return absolute path, jquery object is essentially "class array" structure (similar to arguments), so use [0] to access the original ecological objects, and then take "href"; Console.log ($anchor [0]["href"]); Returns the original path Console.log ($anchor. attr ("href"));