Javascript 获取域名后的部分 URL //... 通过将 URL 拆分为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11898626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:35:47  来源:igfitidea点击:

Get parts of URL after domain name //... by splitting URL into array

javascriptjqueryarrays

提问by Mike Vierwind

I have make this code:

我已经制作了这个代码:

var newURL = $(".list-portfolio a").attr("href"),
    pathArray = newURL.split( '/' ),
    secondLevelLocation = pathArray[0];
console.log(pathArray);

    var pathArray = pathArray[3, 4];

The pathArray value is ["http:", "", "www.mikevierwind.nl", "portfolio", "ruimzicht.html"]

pathArray 值是 ["http:", "", "www.mikevierwind.nl", "portfolio", "ruimzicht.html"]

How can i get the last 2 items of this array. I want that the results is portfolio/ruimzicht.html.

我怎样才能得到这个数组的最后 2 个项目。我希望结果是portfolio/ruimzicht.html。

回答by ninjagecko

You don't need any of this, you just want window.location.pathname:

你不需要任何这些,你只需要window.location.pathname

> window.location.pathname
"/questions/11898626/get-items-of-the-array/11898963"

This will let you in the future have directories like "portfolio/2012/ruimzicht.html", and change domains to say "www.mikevierwind.???" without changing your code.

这将使您将来拥有“portfolio/2012/ruimzicht.html”之类的目录,并将域更改为“www.mikevierwind.???” 无需更改您的代码。



If you are not currently on the domain (and can't do the above), you can do it your way with a one-liner:

如果您当前不在域中(并且无法执行上述操作),则可以使用单行程序按自己的方式进行操作:

> pathArray.slice(-2).join('/')
"portfolio/ruimzicht.html"

But this is not future-proof like the above. To make it future-proof, you can do:

但这不是像上面那样面向未来。为了使其面向未来,您可以执行以下操作:

> url.split(document.domain)[1].slice(1)
"portfolio/2012/ruimzicht.html"

One would do this generally on foreign URLs when you are not currently on the domain and thus can't do window.location.pathname.

当您当前不在域中时,通常会在外国 URL 上执行此操作,因此无法执行window.location.pathname.

回答by jmventar

You could use array length if you don't have a fixed size or number of elements.

如果您没有固定的大小或元素数量,则可以使用数组长度。

var path = array[array.length-2]+'/'+array[array.length-1];

If you just want the path use plain JS or jQuery as they suggest you in comments.

如果您只想要路径使用纯 JS 或 jQuery,因为他们在评论中建议您。

//Plain JS
var path = window.location.pathname;
//jQuery
$(location).attr('pathname'); 

回答by Nick

Or May be something like this

或者可能是这样的

var newURL = $(".list-portfolio a").attr("href"),
pathArray = newURL.split( '/' ),
secondLevelLocation = pathArray.shift();

var pathArray = pathArray.join('/');

回答by yogi

You could try this

你可以试试这个

var newURL = $(".list-portfolio a").attr("href"),
    pathArray = newURL.split( '/' ),
    secondLevelLocation = pathArray[0];
console.log(pathArray);

var pathArray = pathArray[3] +'/'+ pathArray[4];

回答by robby dwi hartanto

try this

尝试这个

console.log(new URL(document.URL));

you would get object of URL

你会得到 URL 的对象