Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the base URL and file path from my windows URL by using Javascript

Hi I tried to get the base URL and file path from my windows URL..But I cant get it..Please correct me..

The URL is:

http://sample.com:30023/portal/site/samples/index.jsp

The current output is: http://sample.com:30023/index.jsp?

The required output is: http://sample.com:30023/portal/site/samples/

Code used :

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";
like image 813
Naju Avatar asked Feb 18 '23 16:02

Naju


1 Answers

add location.pathname, so it becomes

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + location.pathname;

Also, why not simply use location.href to get the whole thing?

Good reference at https://developer.mozilla.org/en-US/docs/DOM/window.location

like image 188
Peter Herdenborg Avatar answered Apr 07 '23 10:04

Peter Herdenborg