Convert relative path to absolute using JavaScript
This should do it:
function absolute(base, relative) {
var stack = base.split("/"),
parts = relative.split("/");
stack.pop(); // remove current file name (or empty string)
// (omit if "base" is the current folder without trailing slash)
for (var i=0; i<parts.length; i++) {
if (parts[i] == ".")
continue;
if (parts[i] == "..")
stack.pop();
else
stack.push(parts[i]);
}
return stack.join("/");
}
How does a browser resolves/constructs an absolute url from a relative one
string ToAbsoluteUrl(params string[] pathParts)
{
return new Uri(Path.Combine(pathParts)).ToString();
}
use it this way:
ToAbsoluteUrl("https://alibabagroup.com/en/global/home", "../ir/home");
IE Not using redirected URL for resolving relative URLs
Since we are processing our HTML/JS in another servlet before the call to the colorScheme servlet, what I did was check to see if the requested skin was dynamic (from the servlet) or not at this stage. If the colorScheme is dynamic, I write out the link tag with the address of the CSS servlet, otherwise I write out the address of the CDN.
How to get a URL by a relative path ? - Javascript
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)
How to extract absolute URL from relative HTML links using Jsoup?
You need Element#absUrl()
.
String url = dl.select("a").absUrl("href");
You can by the way shorten the select:
Document document = Jsoup.connect(url).get();
Elements links = document.select("div.results dl a");
for (Element link : links) {
String url = link.absUrl("href");
}
Converting relative paths to absolute with JavaScript
I'm pretty sure that if you use the href
property instead of getting the attribute, you'll have a full url with domain:
$("a").live('click', function(){
var url = this.href; // use the property instead of attribute
//do something
});
As noted in the question linked by @Phrogz, it sounds as though there are issues with IE6.
If you need to support it, you may need to build the href
from the different parts like this.host
and this.pathname
. Those properties are supported by IE6. There are others you could use too, but you'd need to verify support.
jquery live() function deprecated in version 1.7 and removed from 1.9 so use alternate on():
$("a").on('click', function(){
var url = this.href; // use the property instead of attribute
//do something
});
I need to manually resolve all relative url's in a web page
Unless you're using jQuery or similar I'd just bite the bullet and attack the entire page source.
var entireHTML = document.documentElement.innerHTML;
document.documentElement.innerHTML = entireHTML.replace('href="','href="<path(/)>');
How you determine is up to you, could inject it into your doc.onready function or otherwise inject a global var into the document somewhere.
Related Topics
JavaScript Fuzzy Search That Makes Sense
Nested Element (Web Component) Can't Get Its Template
How to Set Image Source with Base64
Regex for Number with Decimals and Thousand Separator
Stop Cursor from Jumping to End of Input Field in JavaScript Replace
JavaScript to Check When the Browser Window Is Closed
Replace a String in a File with Nodejs
How to Ignore User's Time Zone and Force Date() Use Specific Time Zone
How to Set a Cookie with Expire Time
Jquery Attribute Selector Variable
Switch Statement for String Matching in JavaScript
Get Keys of JSON-Object in JavaScript
React Js: Uncaught (In Promise) Syntaxerror: Unexpected Token < in JSON at Position 0