JavaScript converts a relative address to an absolute address sample code _javascript tips

Source: Internet
Author: User

Looking at the LABJS source code, we find that there is a function to convert the relative address to an absolute address, and take it out as follows:

Copy Code code 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 ' are protocol-relative (begins with//or///), prepend protocol
if (/^\/\/\/?/.test (SRC))
{
src = location.protocol + src;
}
Is ' src ' page-relative? (Not of absolute URL, and not a domain-relative path, beginning with/)
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 to Http://www.inspurstb.com/hzt/scy.js

use JavaScript to convert relative path addresses to absolute paths

1 use image, the test will send a aborted request, and IE6 does not support, the new image will be changed to document.createelement_x_x_x (' 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 uses anchor (link), does not make any request, will only produce the request when joins the DOM, but IE6 also does not support
Copy Code code as follows:

function Getabsoluteurl (URL) {
var a = document.createelement_x_x_x (' a ');
A.href = URL; Sets the relative path to image, and the request is sent
url = a.href; At this point the relative path has become an absolute path
return URL;
}
Getabsoluteurl ("Showroom/list");

3 Use javascript: It's more complicated to implement, here's an example: https://gist.github.com/1088850

The final use is option 2,

This variable, 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's. attr () Method:

Console.log ($anchor [0]["href"]); Returns an absolute path, the jquery object is essentially an array, even if only one, so use [0] to access the original ecological object, and then fetch "href";
Console.log ($anchor. attr ("href")); Return to the original path

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.