Get absolute URL using Javascript

Below is the simple method to get absolute URL using javascript. This code can be run on the Browser Console directly. Open the developer tools and navigate to console tab and paste the below function to test the snippet.

Sample Code Get Absolute URL using Javascript

var getAbsoluteUrl = (function() {
	var a;
	return function(url) {
		if(!a) a = document.createElement('a');
		a.href = url;
		return a.href;

// Sample Result based on the input.
getAbsoluteUrl('/'); //Returns


How to read the current URI in parts?

Let us take an example of the current URL…ing-javascript/ ‎

location.protocol = "http:" = ""

To get the complete URL, we can concat the above code as shown below.

var myURL = window.location.protocol + "//" +  + window.location.pathname;
1 comment
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like