There's a function, which gives me urls like:
./some.css
./extra/some.css
../../lib/slider/slider.css
It's always a relative path.
Let's think we know current path of the page, like http://site.com/stats/2012/
, not sure how do I convert these relative paths to real ones?
We should get something like:
./some.css => http://site.com/stats/2012/some.css
./extra/some.css => http://site.com/stats/2012/extra/some.css
../../lib/slider/slider.css => http://site.com/lib/slider/slider.css
No jQuery, only vanilla javascript.
The href solution only works once the document is loaded (at least in IE11). This worked for me:
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
If you want to make a relative-to-absolute conversion for a link from a custom webpage in your browser (not for the page that runs your script), you can use a more enhanced version of the function suggested by @Bergi:
It'll return
null
if something is wrong.Usage:
The most simple, efficient and correct way to do so it to just use URL api.
Performance wise, this solution is on par with using string manipulation and twice as fast as creating
a
tag.This will work. but only when you open a page with it's file name. it will not work well when you open a link like this
stackoverflow.com/page
. it will work withstackoverflow.com/page/index.php
This works on IE6 too, unlike some other solutions (see Getting an absolute URL from a relative one. (IE6 issue))