简体   繁体   English

如何在 JavaScript 中拆分 url 以获取 url 路径

[英]How to split url to get url path in JavaScript

I have constructed a url path that are pointing to different hostname www.mysite.com , so for example:我构建了一个指向不同主机名www.mysite.com的 url 路径,例如:

var myMainSite = 'www.mymainsite.com' + '/somepath';

so this is equivalent to www.mymainsite.com/path/path/needthispath/somepath .所以这相当于www.mymainsite.com/path/path/needthispath/somepath

How I'm doing it now is like the code below and this gives me a bunch of indexes of the url in the console.log .我现在的做法就像下面的代码,这在console.log给了我一堆 url 的索引。

var splitUrl = myMainSite.split('/');

console.log looks like: console.log看起来像:

0: http://
1: www.
2: mysite.com
3: path
4: path
5: needthispath
6: somepath

and I concat them like splitUrl[5]+'/'+splitUrl[6] and it doesn't look pretty at all.我把它们像splitUrl[5]+'/'+splitUrl[6] ,它看起来一点也不漂亮。

So my question is how to split/remove url location http://www.mymainsite.com/ to get the url path needthispath/somepath in js?所以我的问题是如何在js中拆分/删除url位置http://www.mymainsite.com/来获取url路径needthispath/somepath Is there a quicker and cleaner way of doing this?有没有更快更干净的方法来做到这一点?

First solution (URL object)第一个解决方案(URL 对象)

The URL object can be used for parsing, constructing, normalizing, encoding URLs, and so on. URL 对象可用于解析、构造、规范化、编码 URL 等。

 var url = 'http://www.mymainsite.com/somepath/path2/path3/path4'; var pathname = new URL(url).pathname; console.log(pathname);

The URL interface represents an object providing static methods used for creating object URLs. URL 接口表示一个对象,提供用于创建对象 URL 的静态方法。


Second solution (a kind of a hack)第二种解决方案(一种黑客)

 var urlHack = document.createElement('a'); urlHack.href = 'http://www.mymainsite.com/somepath/path2/path3/path4'; console.log(urlHack.pathname); // you can even call this object with these properties: // protocol, host, hostname, port, pathname, hash, search, origin

Why don't you use the split function and work from there.为什么不使用 split 功能并从那里开始工作。 The split function will break your URL out fully and from there you just need to look for the second last and last items.拆分功能将完全拆分您的 URL,从那里您只需要查找倒数第二个和最后一个项目。

Here is an example:下面是一个例子:

var initial_url = 'http://www.mymainsite.com/path/path/needthispath/somepath';
var url = initial_url .split( '/' );

var updated_url= document.location.hostname + '/' + url[ url.length - 2 ] + '/' + url[ url.length - 1 ];

You can use the URL API, though support is variable.您可以使用URL API,但支持是可变的。

Alternatively, you could use URI.js .或者,您可以使用URI.js

Both allow you to get different parts of an URL, as well as build new URLs from parts.两者都允许您获取 URL 的不同部分,以及从部分构建新的 URL。

Can you please help me with this split? 您能帮我解决这个问题吗? I tried to do it but it doesn't work. 我尝试这样做,但是没有用。

A:\Apis-Utiles\Api-PDFs\src\pdf\111111111.pdf

 function url($url) { var url = $url.split( '//' ); if (url[0] === "http:" || url[0] === "https:") { var protocol = url[0] + "//"; var host = url[1].split( '/' )[0]; url = protocol + host; var path = $url.split(url)[1]; return { protocol: protocol, host: host, path: path }; } } var $url = url("http://www.mymainsite.com/path/path/needthispath/somepath"); console.log($url.protocol); // http:// console.log($url.host); // www.mymainsite.com console.log($url.path); // /path/path/needthispath/somepath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM