[英]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?有没有更快更干净的方法来做到这一点?
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 的静态方法。
Seethe documentation for URL interface on Mozilla MDN请参阅Mozilla MDN 上的 URL 接口文档
The Browser support is pretty good in 2017 (~ 90% but not IE11 nor below)浏览器支持在 2017 年相当不错(~ 90% 但不是 IE11 或以下)
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 ];
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.