Get an Absolute URL with JavaScript
Dealing with URL formats can be a real nightmare. Think of how just a few characters can effect a URL's absolute endpoint:
- starting or not starting with
/
- starting with
//
- starting with
?
- starting with
#
- ...and so on
What if you want an absolute URL though? One that starts with http
or https
? You can use an A
element to get that absolute URL!
The JavaScript
I'm going to use a function that returns a function so that only one A
element is ever created:
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
};
})();
No matter how you pass in the URL string, the URL will come out absolute. Of course strings like `javascript:;` wont come out any different, but real qualified URLs will come out as absolute!
![CSS 3D Folding Animation]()
Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...
![5 HTML5 APIs You Didn’t Know Existed]()
When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It." Can you blame us though? We watched the fundamental APIs stagnate for so long that a basic feature...
![Create a Brilliant Sprited, CSS-Powered Firefox Animation]()
Mozilla recently formally announced Firefox OS and its partners at Mobile World Congress and I couldn't be more excited. Firefox OS is going to change the lives of people in developing countries, hopefully making a name for itself in the US as well. The...
![Create a Twitter AJAX Button with MooTools, jQuery, or Dojo]()
There's nothing like a subtle, slick website widget that effectively uses CSS and JavaScript to enhance the user experience. Of course widgets like that take many hours to perfect, but it doesn't take long for that effort to be rewarded with above-average user retention and...
You can actually even remove the condition, changing the code to
True but you’re creating the element even if
getAbsoluteUrl
is never called.How about
David,
As you pointed out you create the object whether or not its called, so why not do it this way then:
The benefit of this approach is that you don’t execute anything unless
getAbsoluteUrl
is called. Its not a huge improvement, unless you use Immediately Invoked Function Expressions a LOT within your code to create closures.I’m confused as to why the second function? Couldn’t it be removed to be:
Sean,
The second function lets you create the anchor once and reuse it. This saves time and memory.
Thanks Adam, that makes sense :)
Uhhh…
You could also use the
URL()
object.https://developer.mozilla.org/en-US/docs/Web/API/URL
Uhhh…. “This is an experimental technology”
How? Could you post an example? From what I see, the
URL()
constructor cannot autofill the hostname part based on the current page.https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
Hi David. Great idea for a post! To make it better don’t forget that every piece of advice needs a context — so tell the reader where to paste that code — is it in their address bar, do they need to tweet it, or do they need domain specific pre-requisite knowledge to get this to work?
I’m on a “dumb it down please” mission today :)
So is
flexbox
, technically. AndSelection
, andXMLDocument
andHTMLPictureElement
.It’s there. It’s stable and it’s cross browser (IE 10). It also lets you resolve absolute URLs with different domains than the existing page.
Patrick, you’re wrong about IE 10 support. That refers to
URL.createObjectURL()
, not the URL constructor that implements the URLUtils interface. This method works all the way back beyond IE 6, and I’ve been using it for years to parse and resolve URLs. It’s a handy little technique that avoids complex regular expressions, give credit where it’s due!Well, even better then. Nice to know.
Do you have a source for that statement? I just tested Patrick’s example in IE 6 and IE 8 and they crashed with “‘URL’ is undefined”.
Yeah or…
You should warn that you’re using MooTools, @Olmo.
I was being playful with David :P, but if you’d like:
Extending the prototype… a big no-no
MooTools? In 2015??? Well I never…
Why is bad idea to extend the prototype?
This is super easy! worth trying.
Cant this work?
No. You are missing out the host and port.
Will this Work?
Or the Simplest way:
Source: http://mycodingtricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
None of these suggestions worked. I’m still getting the error 404 – file not found when referencing localhost:4200.