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 '/'