如何使用javascript或jQuery获取没有IP或主机名的URL地址?

时间:2017-01-23 06:28:29

标签: javascript jquery

我希望获得没有IP或主机名的URL地址,例如

image.htm?diskindex=0&diskcount=2&sortby=0&view=0&imagefilter=1&sizemore=1

但是window.location.href会返回以下结果。

http://192.168.0.149:9031/image.htm?diskindex=0&diskcount=2&sortby=0&view=0&imagefilter=1&sizemore=1

3 个答案:

答案 0 :(得分:2)

使用pathname属性



window.location.pathname




答案 1 :(得分:2)

像这样。使用split()方法将字符串拆分为数组。

    var url ='http://192.168.0.149:9031/image.htm?diskindex=0&diskcount=2&sortby=0&view=0&imagefilter=1&sizemore=1';//window.location.href
    
    var path = url.split('/');
    alert(path[3]);

答案 2 :(得分:0)

您可以使用URL构造函数,或location .pathname.search



var loc = "http://192.168.0.149:9031/image.htm?diskindex=0&diskcount=2&sortby=0&view=0&imagefilter=1&sizemore=1";

var url = new URL(loc);

var pathSearch = url.pathname.slice(1) + url.search;

console.log(pathSearch);