使用Javascript从我的Windows URL获取基本URL和文件路径

Naj*_*aju 4 html javascript

嗨我试图从我的Windows URL获取基本URL和文件路径..但​​我不能得到它..请纠正我..

URL是:

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

目前的输出是: http ://sample.com :30023/index.jsp

所需的输出是: http ://sample.com :30023/portal/site/samples /

使用的代码:

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";
Run Code Online (Sandbox Code Playgroud)

Pet*_*org 6

添加location.pathname,它就变成了

var baseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + location.pathname;
Run Code Online (Sandbox Code Playgroud)

另外,为什么不简单地使用location.href来获取整个东西呢?

https://developer.mozilla.org/en-US/docs/DOM/window.location上的好参考