在看LABjs源代碼時,發現里面有個將相對地址轉為絕對地址的函數,將其拿出紀錄如下:
復制代碼 代碼如下:
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 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);
}
復制代碼 代碼如下:
function getAbsoluteUrl(url){
var img = new Image();
img.src = url; // 設置相對路徑給Image, 此時會發送出請求
url = img.src; // 此時相對路徑已經變成絕對路徑
img.src = null; // 取消請求
return url;
}
getAbsoluteUrl("showroom/list");
復制代碼 代碼如下:
function getAbsoluteUrl(url){
var a = document.createElement_x_x_x('A');
a.href = url; // 設置相對路徑給Image, 此時會發送出請求
url = a.href; // 此時相對路徑已經變成絕對路徑
return url;
}
getAbsoluteUrl("showroom/list");
新聞熱點
疑難解答
圖片精選