国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript將相對(duì)路徑轉(zhuǎn)絕對(duì)路徑示例

2024-05-06 16:02:46
字體:
供稿:網(wǎng)友
這篇文章主要介紹了javascript將相對(duì)路徑轉(zhuǎn)絕對(duì)路徑示例,這里介紹的其實(shí)本質(zhì)上是兩種方法,通過創(chuàng)建DOM或通過JavaScript計(jì)算,需要的朋友可以參考下

這里介紹的其實(shí)本質(zhì)上是兩種方法,通過創(chuàng)建DOM或通過JavaScript計(jì)算:

1)通過新創(chuàng)建的Image, 經(jīng)測(cè)試會(huì)發(fā)送一個(gè)Aborted的請(qǐng)求,并且IE6不支持, 將new Image改成document.createElement('IMG')也是一樣的;測(cè)試應(yīng)該不喜歡這個(gè)方案;

復(fù)制代碼 代碼如下:


function getAbsoluteUrl(url){
    var img = new Image();
    img.src = url;  // 設(shè)置相對(duì)路徑給Image, 此時(shí)會(huì)發(fā)送出請(qǐng)求
    url = img.src;  // 此時(shí)相對(duì)路徑已經(jīng)變成絕對(duì)路徑
    img.src = null; // 取消請(qǐng)求
    return url;
}
getAbsoluteUrl("showroom/list");

2)創(chuàng)建Anchor(鏈接),這種方法不會(huì)發(fā)出任何請(qǐng)求(請(qǐng)求會(huì)在加入DOM時(shí)產(chǎn)生),但是IE6也不支持

復(fù)制代碼 代碼如下:


/*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, 'http://www.survivalescaperooms.com/')
         .replace(////./.$/, '/../')
         .replace(///?[^//]*/g, function (p) {
      if (p === '/..') {
        output.pop();
      } else {
        output.push(p);
      }
    });
    return output.join('').replace(/^///, input.charAt(0) === 'http://www.survivalescaperooms.com/' ? 'http://www.survivalescaperooms.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) === 'http://www.survivalescaperooms.com/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? 'http://www.survivalescaperooms.com/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('http://www.survivalescaperooms.com/') + 1) + href.pathname) : base.pathname)) +
         (href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
         href.hash;
}



因我們的產(chǎn)品為手機(jī)端網(wǎng)頁,早已不支持IE6,最終使用的是第二種方案;

由此可見,用原生態(tài)的方法訪問所有的Image, Anchor時(shí),返回的都是絕對(duì)路徑,此時(shí)如果想返回原來的相對(duì)路徑,可以用查詢DOM的方法,如jQuery.attr()方法:

復(fù)制代碼 代碼如下:


//返回絕對(duì)路徑,jQuery對(duì)象實(shí)質(zhì)上是"類數(shù)組"結(jié)構(gòu)(類似arguments),因此使用[0]可以訪問到原生態(tài)的對(duì)象,然后取"href";
console.log($anchor[0]["href"]);
//返回原始路徑
console.log($anchor.attr("href"));

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 永新县| 沂南县| 乌兰浩特市| 广宁县| 迁西县| 海盐县| 临夏县| 隆尧县| 收藏| 宁陕县| 嵩明县| 新邵县| 徐州市| 顺义区| 肃宁县| 通许县| 五寨县| 桑植县| 鸡泽县| 福安市| 曲松县| 井研县| 鄂托克前旗| 库车县| 三明市| 三台县| 庆城县| 大兴区| 泾阳县| 尼勒克县| 淮南市| 车致| 商南县| 朝阳区| 个旧市| 蓝山县| 福贡县| 逊克县| 湟源县| 和平区| 故城县|