Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • link.protocol gets me a "http:" if i inspect a anker with "google.com" :-( var link = document.createElement('a'); link.setAttribute('href', 'google.com'); console.log(link.protocol)
    – eXe
    Commented Sep 26, 2016 at 12:35
  • Are you doing that on a http page perhaps? If not specified it will 'inherit' from the current location Commented Nov 14, 2016 at 21:24
  • Thank you for this clever trick! I would like to add one thing: There is both host and hostname. The former includes the port (e.g. localhost:3000), while the latter is only the host's name (e.g. localhost).
    – codener
    Commented Mar 31, 2017 at 11:30
  • This works well in case of absolute URL. It fails in case of Relative URL and cross-browser. Any suggestions?
    – Gururaj
    Commented Aug 4, 2017 at 12:42
  • search = search.split('?')[1]; really?!?! How about search = search.substr(1); instead? Much safer and faster since it creates 1 string instead of 2... Commented Sep 5, 2017 at 3:19