javascript將相對路徑轉絕對路徑樣本

來源:互聯網
上載者:User

 這篇文章主要介紹了javascript將相對路徑轉絕對路徑樣本,這裡介紹的其實本質上是兩種方法,通過建立DOM或通過JavaScript計算,需要的朋友可以參考下

這裡介紹的其實本質上是兩種方法,通過建立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;}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) === '/' ? '/' : '');  }   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")); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.