You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is intentional. There is no way to know whether this is representative of a hostname or path.
Imagine a current URL of: http://example.com/ with a link: <a href="foo">Foo</a>. This would resolve to http://example.com/foo, just how <a href="localhost">Foo</a> would resolve to http://example.com/localhost.
When the String used to construct a new URL does not have enough information to determine whether is absolute or scheme-relative, host-relative, it is assumed to just be a relative URL.
Don't agree. If I go to localhost in Google Chrome and type: document.location in the console, host is handled properly.
if you follow a more canonical example like the solution proposal at http://stackoverflow.com/a/736970/88231 it doesn't work either.
That's because Chrome has context. When you type "localhost" in the URL bar, it assumes you meant "http://localhost", because that's much more likely to be your intent than "file:///localhost" or something else. So Chrome browses to the hostname "localhost", and window.location then operates in the context of the hostname "localhost", path "/".
A generic URL parsing library is not a browser, and can't make browser-like assumptions about user intent, because the prupose of a generic URL parsing library is to parse URLs, not to display web pages to users. Given that the other non-browser-related generic URL parsing libraries I've tried have treated "localhost" as a path rather than a hostname, I think this behavior is well established, and violating it would violate the principle of least surprise.
A BrowserURL class that extends URL and provides more browser-like URL behavior would be well within its rights to interpret "localhost" as a hostname, however.
A browser's Address Bar is afforded more contextual information in that it assumes a String which does not begin with a "/" is assumed to be the hostname and "http://" can be implied. In the case of Chrome, it special-cases "localhost", if you type in "asdf" it will do a Google search for "asdf".
To solve you're particular use-case, URL('http://localhost') or URL('//localhost') will signal that the specified String should be treated as a hostname.
Is there a solution to this issue yet? I'd like to fix it myself, but my regexp is pretty limited and it seems URL_ABSOLUTE_REGEX is the variable that needs to be modified for this to work correctly
@dunkdt It has not been resolved yet. I'd like to eventually revisit this project and change the core rexexes that it uses. I just haven't had time to dedicate to doing that since it is currently meeting my needs.
Activity
ericf commentedon Feb 7, 2012
This is intentional. There is no way to know whether this is representative of a hostname or path.
Imagine a current URL of:
http://example.com/with a link:<a href="foo">Foo</a>. This would resolve tohttp://example.com/foo, just how<a href="localhost">Foo</a>would resolve tohttp://example.com/localhost.When the String used to construct a new
URLdoes not have enough information to determine whether is absolute or scheme-relative, host-relative, it is assumed to just be a relative URL.rgrove commentedon Feb 7, 2012
This seems like the right behavior to me. Other URL parsing libs do the same thing.
Node.js:
Ruby:
srw commentedon Feb 7, 2012
Don't agree. If I go to localhost in Google Chrome and type: document.location in the console, host is handled properly.
if you follow a more canonical example like the solution proposal at http://stackoverflow.com/a/736970/88231 it doesn't work either.
rgrove commentedon Feb 7, 2012
That's because Chrome has context. When you type "localhost" in the URL bar, it assumes you meant "http://localhost", because that's much more likely to be your intent than "file:///localhost" or something else. So Chrome browses to the hostname "localhost", and
window.locationthen operates in the context of the hostname "localhost", path "/".A generic URL parsing library is not a browser, and can't make browser-like assumptions about user intent, because the prupose of a generic URL parsing library is to parse URLs, not to display web pages to users. Given that the other non-browser-related generic URL parsing libraries I've tried have treated "localhost" as a path rather than a hostname, I think this behavior is well established, and violating it would violate the principle of least surprise.
A BrowserURL class that extends URL and provides more browser-like URL behavior would be well within its rights to interpret "localhost" as a hostname, however.
ericf commentedon Feb 7, 2012
A browser's Address Bar is afforded more contextual information in that it assumes a String which does not begin with a
"/"is assumed to be the hostname and"http://"can be implied. In the case of Chrome, it special-cases"localhost", if you type in"asdf"it will do a Google search for"asdf".To solve you're particular use-case,
URL('http://localhost')orURL('//localhost')will signal that the specified String should be treated as a hostname.srw commentedon Feb 7, 2012
Sorry but I wrongly wrote the original issue.
I rechecked and It is:
even if I use 'http://localhost'
ericf commentedon Feb 7, 2012
@srw Ah okay. I can verify that this is in fact happening: http://jsfiddle.net/ericf/JhPCH/
dangmai commentedon Jul 4, 2012
Is there a solution to this issue yet? I'd like to fix it myself, but my regexp is pretty limited and it seems URL_ABSOLUTE_REGEX is the variable that needs to be modified for this to work correctly
ericf commentedon Jul 5, 2012
@dunkdt It has not been resolved yet. I'd like to eventually revisit this project and change the core rexexes that it uses. I just haven't had time to dedicate to doing that since it is currently meeting my needs.
srw commentedon Jul 5, 2012
I tried also with http://code.google.com/p/jsuri/ may be good to integrate all URI's parsers? I don't see that competition is good in this terrain!
ericf commentedon Jul 5, 2012
@srw Yup! I want to replace the guts to use http://blog.stevenlevithan.com/archives/parseuri
ericf commentedon Jul 5, 2012
Also, I found out about jsURI after I wrote this project :P
awb commentedon Jul 13, 2013
I also consider it a bug that
URL("http://localhost").host()returnsundefined.