15

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!

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Bostjan
  • 151
  • 1
  • 1
  • 3

5 Answers5

32

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/

Lucas
  • 16,930
  • 31
  • 110
  • 182
AHMED RABEE
  • 467
  • 6
  • 13
12

This should do it

location.href.replace(/https?:\/\//i, "")
jitter
  • 53,475
  • 11
  • 111
  • 124
  • This works 99% of the time. However it doesn't work if the current Url is e.g. `http://somesite.com/query=http://someothersite.com/blahblah`. All instances of "http://" will be replaced not just the first occurance. Doesn't work for 'https://' too. – o.k.w Nov 08 '09 at 13:39
  • 3
    Fixed it. Handles http/https + caseinsensitve matching. Also replaces only first occurrence. – jitter Nov 08 '09 at 13:48
4

Use document.location.host instead of document.location.href. That contains only the host name and not the full URL.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I am not sure why in the world everyone suggests regular expressions when the location object already does it for you as Gumbo suggested. Documentation is a great thing: https://developer.mozilla.org/En/DOM/Window.location – epascarello Nov 08 '09 at 14:52
  • 1
    Oh yes, it is easier to use "str = location.host + location.path + location.search". How about port number? – o.k.w Nov 08 '09 at 15:21
  • 1
    @o.k.w: Bostjan apparently just wants to know the host and not the full URL. – Gumbo Nov 08 '09 at 16:02
0

Use the URL api

A modern way to get a part of the URL can be to make a URL object from the url that you are given.

const { hostname } = new URL('https://www.some-site.com/test'); // www.some-site.com 

You can of course just pass window location or any other url as an argument to the URL constructor.

Like this

const { hostname } = new URL(document.location.href);
Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
-1

Using javascript replace via regex matching:

javascript:q=(document.location.href.replace(/(https?|file):\/\//,''));void(open('http://website.com/search.php?search='+q,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));

Replace (https?|file) with your choice, e.g. ftp, gopher, telnet etc.

o.k.w
  • 25,490
  • 6
  • 66
  • 63
  • actually this won't work for file as that protocol uses three slashes – jitter Nov 08 '09 at 13:49
  • @jitter: "file" wasn't supposed to be included, it has no practical usage. It was added while I was testing the code locally on my machine. Anyway, doesn't really matter :) – o.k.w Nov 08 '09 at 15:30