5
var baseURL = "http://google.com/a/b/c/d.html";
var relativePath = "../../e.mp3";

I want get result http://google.com/a/e.mp3 by baseURL and relativePath

Is there an easy way to do it?

4
  • did you tried the example by me Commented Apr 19, 2014 at 6:21
  • @KailashYadav Yeah, It's awesome and I +1 for it. Thanks Commented Apr 19, 2014 at 6:37
  • Great to hear. Please accept as answer then too :) Commented Apr 19, 2014 at 6:39
  • @KailashYadav I will, Just like to see if someone has another way to do it Commented Apr 19, 2014 at 6:46

3 Answers 3

4

You can use this function:

function resolve(url, base_url) {


  var doc      = document
    , old_base = doc.getElementsByTagName('base')[0]
    , old_href = old_base && old_base.href
    , doc_head = doc.head || doc.getElementsByTagName('head')[0]
    , our_base = old_base || doc_head.appendChild(doc.createElement('base'))
    , resolver = doc.createElement('a')
    , resolved_url
    ;
  our_base.href = base_url;
  resolver.href = url;
  resolved_url  = resolver.href; // browser magic at work here

  if (old_base) old_base.href = old_href;
  else doc_head.removeChild(our_base);
  return resolved_url;
}
alert(resolve('../../e.mp3', 'http://google.com/a/b/c/d.html'));

Here is the fiddle link:

http://jsfiddle.net/ecmanaut/RHdnZ/

Here it is user for something like same: Getting an absolute URL from a relative one. (IE6 issue)

Sign up to request clarification or add additional context in comments.

1 Comment

When just coping from another question stackoverflow.com/questions/470832 you could at least give credit to the original author.
0

One way of doing it:

If you use Node.js you can use the path module to normalize your path. If you don't use Node.js you can still use this module in the browser with the help of browserify.

Obviously you would need to get rid of d.html from the baseUrl and append the relativePath before you call path.normalize.

Comments

-1

Take a look at this Gist. It worked quite well for me as it covers also special cases where simpler solutions will fail.

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;
}

You also need this helper function:

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.