admin管理员组文章数量:1332638
This bookmarklet is working as expected.
javascript:{window.location='='+encodeURIComponent(window.location.href)}
But it sends the entire URL to bing. Instead I need only the last part to be sent. For e.g. If I am on the page
.aspx
Then it should send only "aa286483.aspx" to bing. Is this possible?
This bookmarklet is working as expected.
javascript:{window.location='http://bing./search?q='+encodeURIComponent(window.location.href)}
But it sends the entire URL to bing. Instead I need only the last part to be sent. For e.g. If I am on the page
https://msdn.microsoft./en-us/library/aa286483.aspx
Then it should send only "aa286483.aspx" to bing. Is this possible?
Share Improve this question asked Apr 30, 2017 at 12:46 shantanuoshantanuo 32.2k91 gold badges263 silver badges431 bronze badges4 Answers
Reset to default 4
function getFileName(url) {
return url.split("/").pop();
}
var url = "https://msdn.microsoft./en-us/library/aa286483.aspx";
console.log(getFileName(url));
And note that you should take care of url parameters (aa286483.aspx/?id=2 for example) too if it is important in your case. I don't include it here since your question is about file name only.
There is window.location.pathname
, which will return /en-us/library/aa286483.aspx
.
A direct approach it will be:
window.location.pathname.split('/').slice(-1)[0]
Just read past the last path delimiter?
var url = window.location.pathname;
var last = url.substr(url.lastIndexOf("/") + 1);
you can do:
var array = window.location.href.split('/'),
res = "";
while(res === "") {
res = array.pop();
}
return res;
this will prevent you from returning an empty string if the URL ends with '/'
本文标签: javascriptExtract page name from URLStack Overflow
版权声明:本文标题:javascript - Extract page name from URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742341928a2456774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论