JavaScript turns relative path to absolute path Sample _ Basics

Source: Internet
Author: User
Tags hash

Essentially, there are two ways to do this, either by creating the DOM or by using javascript:

1 through the newly created image, the test will send a aborted request, and IE6 does not support, the new image will be changed to Document.createelement (' IMG ') is the same; the test should not like this program;

Copy Code code as follows:

function Getabsoluteurl (URL) {
var img = new Image ();
img.src = URL; Sets the relative path to image, and the request is sent
url = img.src; At this point the relative path has become an absolute path
IMG.SRC = null; Cancel Request
return URL;
}
Getabsoluteurl ("Showroom/list");

2 Create anchor (link), this method does not make any request (the request will be generated when the DOM is added), but IE6 also does not support

Copy Code code as follows:

/*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 the handset end webpage, already does not support IE6, the final use is the second kind of plan;

Thus, using the original ecological method to access all the image, anchor, the return is absolute path, at this point if you want to return to the original relative path, you can query the DOM method, such as Jquery.attr () method:

Copy Code code as follows:

Returns an absolute path in which the JQuery object is essentially an "array of classes" structure (similar to arguments), so that [0] can be used to access an object of the original ecology and then "href";
Console.log ($anchor [0]["href"]);
Return to 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.