JavaScript turns relative path to absolute path example

Source: Internet
Author: User
Tags hash relative

  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"));  
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.