Example of converting relative paths to absolute paths in javascript _ basic knowledge-js tutorial

Source: Internet
Author: User
This article mainly introduces the example of converting relative paths to absolute paths in javascript. In essence, this article describes two methods: Creating DOM or using JavaScript computing, you can refer to the following two methods to create a DOM or perform JavaScript computing:

1) if you use the newly created Image, a test will send an Aborted request, and IE6 does not support it. Change new Image to document. the same is true for createElement ('img '). 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 is sent.
Url = img. src; // The relative path has become an absolute path.
Img. src = null; // cancel the request
Return url;
}
GetAbsoluteUrl ("showroom/list ");

2) create an Anchor (Link). This method will not send any request (the request will be generated when the DOM is added), but IE6 does not support

The Code is 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 products are mobile Web pages, we have long been unable to support IE6, and we are using the second solution;

It can be seen that when accessing all images and Anchor using the original ecological method, all returned absolute paths. To return the original relative paths, you can use the DOM query method, such as jQuery. attr () method:

The Code is as follows:


// Return 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 and then take "href ";
Console. log ($ anchor [0] ["href"]);
// Return 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.