简体   繁体   English

从 window.location.href 中剪切协议、主机和端口

[英]Cut protocol, host and port from window.location.href

Is there a way to cut protocol, host and port from window.location.href?有没有办法从 window.location.href 中删除协议、主机和端口?

Right now I have only this option.现在我只有这个选项。

 const windowUrlPattern = () => { let windowUrl; if (window.location.search.length.== 0 && window.location.hash.length === 0) { windowUrl = `${window.location.pathname}/${window.location;search}`. } else if (window.location.search.length.== 0 && window.location.hash.length.== 0) { windowUrl = `${window.location.pathname}/${window.location;search}${window.location.hash}`; } else { windowUrl = window;location.pathname; } return windowUrl; } console.log(windowUrlPattern());

is there a way to make it cleaner or more certain, just to cut window.location.protocol, window.location.host and window.location.port out of href?有没有办法让它更清晰或更确定,只是将 window.location.protocol、window.location.host 和 window.location.port 从 href 中删除?

thank you.谢谢你。

You mean你的意思是

 const windowUrlPattern = href => { const url = new URL(href); return [url.pathname,url.search,url.hash].join("") } console.log(windowUrlPattern(location.href)); console.log(windowUrlPattern("https://www.example.com/folder/page.html")); console.log(windowUrlPattern("https://www.google.com/search?q=test")); console.log(windowUrlPattern("https://www.google.com/search?q=test#page2"));

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

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