javascript将相对路径转绝对路径的例子
来源:互联网 发布:淘宝双11促销海报 编辑:程序博客网 时间:2024/05/16 01:23
javascript将相对路径转绝对路径示例,这里介绍的其实本质上是两种方法,通过创建DOM或通过JavaScript计算。
/*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) === '/' ? '/' : '');
}
// www.jbxue.com
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;
}
因我们的产品为手机端网页,早已不支持IE6,最终使用的是第二种方案;
这里介绍的其实本质上是两种方法,通过创建DOM或通过JavaScript计算:
1)通过新创建的Image, 经测试会发送一个Aborted的请求,并且IE6不支持, 将new Image改成document.createElement('IMG')也是一样的;测试应该不喜欢这个方案;
复制代码代码如下:
function getAbsoluteUrl(url){
var img = new Image();
img.src = url; // 设置相对路径给Image, 此时会发送出请求
url = img.src; // 此时相对路径已经变成绝对路径
img.src = null; // 取消请求
return url;
} // www.jbxue.com
getAbsoluteUrl("showroom/list");
var img = new Image();
img.src = url; // 设置相对路径给Image, 此时会发送出请求
url = img.src; // 此时相对路径已经变成绝对路径
img.src = null; // 取消请求
return url;
} // www.jbxue.com
getAbsoluteUrl("showroom/list");
2)创建Anchor(链接),这种方法不会发出任何请求(请求会在加入DOM时产生),但是IE6也不支持
复制代码代码如下:
/*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) === '/' ? '/' : '');
}
// www.jbxue.com
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;
}
因我们的产品为手机端网页,早已不支持IE6,最终使用的是第二种方案;
由此可见,用原生态的方法访问所有的Image, Anchor时,返回的都是绝对路径,此时如果想返回原来的相对路径,可以用查询DOM的方法,如jQuery.attr()方法:
复制代码代码如下:
//返回绝对路径,jQuery对象实质上是"类数组"结构(类似arguments),因此使用[0]可以访问到原生态的对象,然后取"href";
console.log($anchor[0]["href"]);
//返回原始路径
console.log($anchor.attr("href"));
console.log($anchor[0]["href"]);
//返回原始路径
console.log($anchor.attr("href"));
0 0
- javascript将相对路径转绝对路径的例子
- 将相对路径转绝对路径
- 将网页的相对路径转为绝对路径
- 将相对路径转为绝对路径的方法
- 使用 JavaScript 将相对路径转换为绝对路径
- 相对路径转绝对路径
- Java爬虫的相对路径转绝对路径
- 绝对路径相对路径的比较
- 将网站绝对路径更改为相对路径
- 将网站绝对路径更改为相对路径
- C# 将绝对路径转换为相对路径
- C#相对路径转绝对路径,绝对路径转相对路径
- URL相对路径转绝对路径
- URL相对路径转绝对路径
- C# 相对路径转绝对路径
- php&javascript 中的 绝对路径与相对路径
- jsp的路径(相对路径、绝对路径)
- 相对路径、根路径、绝对路径的区别
- Unique Paths I & II
- substr 扩展版:支持中文字符串截取
- STL中queue,list,vector频繁访问效率
- java.util.ConcurrentModificationException
- Minimum Path Sum
- javascript将相对路径转绝对路径的例子
- iOS开发(1)写在前面的话
- 【复杂网络系列】社团发现简介
- MYSQL 随机查询
- 准确率(Precisio)、召回率(Recall)、F1-Measure
- transition状态下Mecanim动画的跳转
- 用户名正则匹配
- JAVA 设计模式
- windows安装eclipse c++