我有一个类似http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235的URL。
我想获得不带查询字符串的URL: http://localhost/dms/mduserSecurity/UIL/index.php。
JavaScript中有没有这种方法?目前我正在使用document.location。href,但它返回完整的URL。
我有一个类似http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235的URL。
我想获得不带查询字符串的URL: http://localhost/dms/mduserSecurity/UIL/index.php。
JavaScript中有没有这种方法?目前我正在使用document.location。href,但它返回完整的URL。
当前回答
这个怎么样:location。href。切片(0,- (location。Search + location.hash).length))
其他回答
Try:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注意:.host而不是.hostname,这样端口也包括在内,如果需要的话)
这里有两种方法:
<script type="text/javascript">
var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
=true&pcode=1235";
var st=s.substring(0, s.indexOf("?"));
alert(st);
alert(s.replace(/\?.*/,''));
</script>
使用window.location的属性
var loc = window.location;
var withoutQuery = loc.hostname + loc.pathname;
var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;
您可以在https://developer.mozilla.org/en/DOM/window.location上查看更多属性
只需使用split(简单的方法)切割字符串:
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
阅读Window。location和location接口:
const urlPieces = [location.]协议,'//',位置。主机,location.pathname] let url = urlPieces.join(") console.log ({urlPieces、url})