Get the current url but without the http:// part bookmarklet!

Bostjan picture Bostjan · Nov 8, 2009 · Viewed 19.2k times · Source

Guys I have a question, hoping you can help me out with this one. I have a bookmarklet;

javascript:q=(document.location.href);void(open('http://other.example.com/search.php?search='+location.href,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));

which takes URL of the current webpage and search for it in another website. When I use this bookmarklet it takes the whole URL including http:// and searches for it. But now I would like to change this bookmarklet so it will take only the www.example.com or just example.com (without http://) and search for this url. Is it possible to do this and can you please help me with this one?

Thank you!

Answer

AHMED RABEE picture AHMED RABEE · Aug 27, 2012

JavaScript can access the current URL in parts. For this URL:

http://css-tricks.com/example/index.html

window.location.protocol = "http"

window.location.host = "css-tricks.com"

window.location.pathname = "/example/index.html"

please check: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/