Search code examples
javascriptbookmarklet

Extract page name from URL


This bookmarklet is working as expected.

javascript:{window.location='http://bing.com/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.com/en-us/library/aa286483.aspx

Then it should send only "aa286483.aspx" to bing. Is this possible?


Solution

  • 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]