Asked 7 years ago
4 Jan 2017
Views 2571
web-api

web-api posted

How to get current URL in JavaScript ?

how to get current URL by JavaScript or by jQuery

suppose i am browsing http://www.example.com/direct/json.html#hash_value

it i should get value of Url exact :: http://www.example.com/direct/json.html . 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 Nov 30 '-1 00:00

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;
 alert(cuurent_url_1);
 alert(cuurent_url_2);

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

console.log(window.location);

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,"");
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
Nilesh

Nilesh
answered Nov 30 '-1 00:00

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 window.location.search and document.location.search

without hash . with query string URL


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


so suppose current question url :: http://arrayoverflow.com/question/how-to-get-current-url-in-javascript/230#mehased

console.log("output :: for current url -- "+window.location.origin +window.location.pathname );
// output :: for current url -- http://arrayoverflow.com/question/how-to-get-current-url-in-javascript/230
console.log("output :: for current url -- "+document.location.origin +document.location.pathname );
// output :: for current url -- http://arrayoverflow.com/question/how-to-get-current-url-in-javascript/230
console.log("output :: for current url -- "+document.location.origin +document.location.pathname +document.location.search);
// output :: for current url -- http://arrayoverflow.com/question/how-to-get-current-url-in-javascript/230


shabi

shabi
answered Nov 30 '-1 00:00

websanova/js-url will help you on this . it will give all schema of the url.

https://github.com/websanova/js-url/blob/master/url.js
try to grab the url.js and insert it at your page.

//believing that you embed url.js 
console.log(url("hostname")+url("path"));


Rasi

Rasi
answered Nov 30 '-1 00:00

using jQuery ::


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

console.log(spliturl[0]);


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

$("*").context.baseURI
$("*").context.documentURI
$("*").context.URL
context object schema


$("*").context.domain give you domain name
shyam

shyam
answered Nov 30 '-1 00:00


document.documentURI give current url of the webpage


console.log(document.documentURI);//http://arrayoverflow.com/question/how-to-get-current-url-in-javascript/230
Post Answer