Asked 17 days ago
4 Jan 2017
Views 878

web-api posted

How to get current URL in JavaScript ?

how to get current URL by JavaScript or by jQuery

suppose i am browsing

it i should get value of Url exact :: . if there is #hash_value than it should be removed.
why need jQuery , current url will be get by pure JavaScript very well. - shabi  
Jan 16 '17 00:09
Mitul Dabhi

Mitul Dabhi
answered Jan 21 '18 20:17

i dont think its big deal .
one can use document.location
or window.location
it will give you current browsed URL or current web URL where JavaScript is running

 var cuurent_url_1=document.location;
 var cuurent_url_2=window.location;

it give us two alert one by one for URL with hash

But it is not good practice to use for getting URL as string because its actually return an Object


will give us

Location {hash: "#r&#removehash", search: "?d=", pathname: "/test/test3.php", port: "", hostname: "localhost"…}

it means alert(window.location) , alert(document.location) is not what we need
correct possible way to get string url of current page by JavaScript

var url_1=document.location.href; 
var url_2=document.location.toString();
var url_3 = window.location.toString();
var url_4 = window.location.href();

remove hash from current URL by JavaScript
document.location.hash or window.location.hash give you hash string to remove from original URL

var url_without_hash=document.location.href.replace(document.location.hash,"");
var url_without_hash=window.location.href.replace(window.location.hash,"");
Edit Answer
yes its true use document.location.toString() not direct document.location . it seem it have location of URL but it generate problem when you code by assuming string but it really a object . i found a situation where it keep redirecting by using direct document.location like string . - Phpworker  
Jan 9 '17 00:59

answered Jan 21 '18 20:17

window.location and document.location have property
origin -- give hostname with http:// or https://
pathname -- give the file name
so following code give without hash url , without query string

var url= window.location.origin +window.location.pathname;
var url=document.location.origin +document.location.pathname;

to add query string append and

without hash . with query string URL

var url=window.location.origin;
var url=document.location.origin;

so suppose current question url ::

console.log("output :: for current url -- "+window.location.origin +window.location.pathname );
// output :: for current url --
console.log("output :: for current url -- "+document.location.origin +document.location.pathname );
// output :: for current url --
console.log("output :: for current url -- "+document.location.origin +document.location.pathname;
// output :: for current url --

Edit Answer

answered Jan 21 '18 20:17

websanova/js-url will help you on this . it will give all schema of the url.
try to grab the url.js and insert it at your page.

//believing that you embed url.js 

Edit Answer

answered Jan 21 '18 20:17

using jQuery ::

var url = $("*").context.baseURI;// give absolute url
var spliturl=url.split("#");// try to remove hash


in jQuery baseURI , documentURI , URL in context. give full URL address

context object schema

$("*").context.domain give you domain name
Edit Answer

answered Jan 21 '18 20:17

document.documentURI give current url of the webpage

Edit Answer
Post Answer