Example code _ JavaScript tips-js tutorial

Source: Internet
Author: User
This article describes how to convert a relative address to an absolute address in JavaScript. The specific example is as follows. If you are interested, refer to the following, I hope it will be helpful to you. When you look at the LABjs source code, you will find a function that converts the relative address to the absolute address. The record is as follows:

The 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? 1.1.23 ") 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;

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) Using Anchor (Link) will not send any request, but will only generate a request when adding the DOM, but IE6 does not support

The 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

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.