When reading the LABjs source code, I found a function that converts the relative address to the absolute address. The record is as follows:
Copy codeThe Code is as follows:
Function canonical_uri (src, base_path)
{
Var root_page =/^ [^? #] * \ //. Exec (location. href) [0],
Root_domain =/^ \ w + \:\/\/\/? [^ \/] +/. Exec (root_page) [0],
Absolute_regex =/^ \ w + \:\/\//;
// Is 'src' is protocol-relative (begins with // or //), prepend protocol
If (/^ \/\/\/? /. Test (src ))
{
Src = location. protocol + src;
}
// Is 'src' page-relative? (Not an absolute URL, and not a domain-relative path, beginning /)
Else if (! Absolute_regex.test (src) & src. charAt (0 )! = "/")
{
// Prepend 'base _ path', if any
Src = (base_path | "") + src;
}
// Make sure to return 'src' as absolute
Return absolute_regex.test (src )? Src: (src. charAt (0) = "/"? Root_domain: root_page) + src );
}
If the current page address is: http://www.inspurstb.com/hzt/index.html
Then canonical_uri ("scy. js") returns http://www.inspurstb.com/hzt/scy.js
Use Javascript to convert relative paths to absolute paths
1) if Image is used, a test will send an Aborted request, and IE6 does not support it. Change new Image to document. the same is true for createElement_x_x_x ('img '). The test should dislike this scheme;
Copy codeThe 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) Using Anchor (Link) will not send any request, but will only generate a request when adding the DOM, but IE6 does not support
Copy codeThe Code is as follows:
Function getAbsoluteUrl (url ){
Var a = document. createElement_x_x_x ('A ');
A. href = url; // set the relative path to the Image. a request is sent.
Url = a. href; // The relative path has become an absolute path.
Return url;
}
GetAbsoluteUrl ("showroom/list ");
3) using JavaScript: implementation is more complex, here is an example: https://gist.github.com/1088850
Option 2,
As a result, when accessing all images and Anchor using the original method, all absolute paths are returned. To return the original relative paths, you can use the DOM query method, such as jQuery's. attr () method:
Console. log ($ anchor [0] ["href"]); // returns the absolute path. jQuery objects are essentially arrays, even if there is only one, therefore, you can use [0] to access the original object and then use "href ";
Console. log ($ anchor. attr ("href"); // return the original path