This article mainly introduces the example of converting relative paths to absolute paths in javascript. In essence, there are two methods. By creating DOM or using JavaScript computing, you can refer
In essence, this article describes two methods: creating a DOM or using JavaScript: 1) using a newly created Image, a test will send an Aborted request, and IE6 does not support it, change new Image to document. createElement ('img ') is the same. The test should not like this scheme. The Code is as follows: function getAbsoluteUrl (url) {var IMG = new Image (); img. src = url; // set the relative path to the Image. A request url = img will be sent. src; // The relative path has changed to the absolute path img. src = null; // cancel the request return url;} getAbsoluteUrl ("showroom/list"); 2) create Anchor (Link ), this method does not send 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, indent: 2 */function parseURI (url) {var m = String (url ). replace (/^ \ s + | \ s + $/g ,''). match (/^ ([^ :\/? #] + :)? (\/\/(?: [^: @] * (?: [^: @] *)? @)? ([^ :\/? #] *) (?: :( \ D *))?))? ([^? #] *) (\? [^ #] *)? (# [\ S \ S] *)? /); // 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) {var output = []; input. replace (/^ (\. \.? (\/| $) + /,''). Replace (/\/(\. (\/| $) +/g ,'/'). replace (/\/\. \. $ /,'/.. /'). replace (/\/? [^ \/] */G, function (p) {if (p = '/.. ') {output. pop ();} else {output. push (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. authority: base. authority) + removeDotSegments (href. protocol | href. authority | href. pathname. 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) + href. hash;} because our product is a mobile web page, IE6 is no longer supported, and the second solution is used. It can be seen that when accessing all images and Anchor in the original ecological way, the returned result is an absolute path. To return the original relative path, you can use the DOM query method, such as jQuery. attr () method: the code is as follows: // returns the absolute path. The jQuery object is essentially a "class array" structure (similar to arguments). Therefore, you can use [0] to access the original object, then take "href"; console. log ($ anchor [0] ["href"]); // return to the original console path. log ($ anchor. attr ("href "));