TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

IE seems right, Chrome seems wrong

166 点作者 jessepollak超过 11 年前

15 条评论

bzbarsky超过 11 年前
The spec on this is at <a href="http://url.spec.whatwg.org/#dom-url-host" rel="nofollow">http:&#x2F;&#x2F;url.spec.whatwg.org&#x2F;#dom-url-host</a> and says:<p><pre><code> If url is null, return the empty string. If port is the empty string, return host, serialized. Return host, serialized, &quot;:&quot;, and port concatenated. </code></pre> So what is port in this case? That&#x27;s defined at <a href="http://url.spec.whatwg.org/#port-state" rel="nofollow">http:&#x2F;&#x2F;url.spec.whatwg.org&#x2F;#port-state</a> and the key step is:<p><pre><code> 2. If buffer is equal to url&#x27;s scheme&#x27;s default port, set buffer to the empty string. </code></pre> So IE is wrong and the MDN documentation is misleading. I&#x27;ve fixed the latter; can&#x27;t do much about IE. ;)
评论 #6529113 未加载
评论 #6529142 未加载
评论 #6531318 未加载
评论 #6529700 未加载
评论 #6529327 未加载
jgraham超过 11 年前
So there are a few steps here. First<p><pre><code> parser.href = url; </code></pre> causes the URL to be parsed. When URLs are parsed, the spec [1] says:<p><pre><code> 2. If buffer is equal to url&#x27;s scheme&#x27;s default port, set buffer to the empty string. 3. Set url&#x27;s port to buffer. </code></pre> Then the host is serialized. Here the spec says:<p><pre><code> 2. If port is the empty string, return host, serialized. </code></pre> Since is is the empty string in the case where the port is the default port, no port should be appended on output, even if there was one there on input (serializing host doesn&#x27;t magically append a port).<p>I&#x27;m not really clear why this normalise function is needed at all though? Origin should be string comparable?<p>[1] <a href="http://url.spec.whatwg.org/#port-state" rel="nofollow">http:&#x2F;&#x2F;url.spec.whatwg.org&#x2F;#port-state</a><p>[2] <a href="http://url.spec.whatwg.org/#dom-url-host" rel="nofollow">http:&#x2F;&#x2F;url.spec.whatwg.org&#x2F;#dom-url-host</a>
angularly超过 11 年前
If you ever find yourself considering whether it&#x27;s IE or the other browsers, that are doing it wrong... just assume IE is wrong, and you will most likely be right.
评论 #6529993 未加载
评论 #6531340 未加载
syncerr超过 11 年前
As a note to others using document.createElement to manipulate URLs. While it seems like a neat tool, it is buggy in IE in other ways[1]. I would advise using a cross-browser library (or writing your own):<p>- <a href="https://code.google.com/p/jsuri/" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;jsuri&#x2F;</a><p>- <a href="http://medialize.github.io/URI.js/" rel="nofollow">http:&#x2F;&#x2F;medialize.github.io&#x2F;URI.js&#x2F;</a><p>[1] <a href="http://stackoverflow.com/questions/956233/javascript-pathname-ie-quirk" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;956233&#x2F;javascript-pathnam...</a>
chavesn超过 11 年前
One of my most popular answers on Stack Overflow has been about exactly these properties. This trick you are using is new to me and you&#x27;ve helped me improve the answer so it works for any URL. Thank you!<p>Updated answer here:<p><a href="http://stackoverflow.com/questions/6944744/javascript-get-portion-of-url-path/6944772#6944772" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;6944744&#x2F;javascript-get-po...</a><p>I also created a JSFiddle to test here:<p><a href="http://jsfiddle.net/nchaves/vMrjs/2/" rel="nofollow">http:&#x2F;&#x2F;jsfiddle.net&#x2F;nchaves&#x2F;vMrjs&#x2F;2&#x2F;</a>
评论 #6530316 未加载
thezilch超过 11 年前
Doesn&#x27;t appear it&#x27;s only Chrome that gets it &quot;wrong,&quot; as it is _only_ IE that gets it &quot;right.&quot;<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;HTMLAnchorE...</a> writes:<p><pre><code> URLUtils.host Is a DOMString representing the hostname and port (if it&#x27;s not the default port) in the referenced URL. </code></pre> But it implements <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLUtils" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;URLUtils</a> (experimental), which writes:<p><pre><code> URLUtils.host Is a DOMString containing the host, that is the hostname, a &#x27;:&#x27;, and the port of the URL.</code></pre>
评论 #6529064 未加载
jqueryin超过 11 年前
Correct me if I&#x27;m wrong, but couldn&#x27;t your code just have easily used hostname as opposed to host since that&#x27;s what you were specifically interested in?
评论 #6529176 未加载
pfraze超过 11 年前
Unrelated to the article, very cool bit of animation on the site&#x27;s header. I like it!
评论 #6529291 未加载
评论 #6529233 未加载
评论 #6529022 未加载
评论 #6529881 未加载
评论 #6529097 未加载
purplerails超过 11 年前
Quick question: why do you need to normalize the origin? Is it ever passed in by the browser runtime un-normalized?<p>i..e, why couldn&#x27;t you have written:<p><pre><code> if (e.origin !== &#x27;https:&#x2F;&#x2F;clef.io&#x27;) {</code></pre>
评论 #6530267 未加载
JimmaDaRustla超过 11 年前
The URL is being passed into an &lt;a&gt; tag href value.<p>Checking Mozilla documentation, it makes it quite clear. &quot;Is a DOMString representing the hostname and port (if it&#x27;s not the default port) in the referenced URL.&quot;<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;HTMLAnchorE...</a><p>So IE is getting it wrong? Edit: Other post points out experimental standard which IE may be adopting.
dragontamer超过 11 年前
<a href="http://lwn.net/Articles/551695/" rel="nofollow">http:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;551695&#x2F;</a><p>Its funny how standards evolve. Should we do things because they were declared standard? Or should we do things because they make sense?<p>In the case of the original post, it was IE vs Chrome. In the case I just brought up, it is Linux vs BSD with their implementation of POSIX.<p>The same story will continue to run as long as we expect different systems to run &quot;similarly&quot; based on a unified standard.
Kerrick超过 11 年前
I ran into this when developing with postMessage as well. This Stack Overflow post pointed me in the right direction: <a href="http://stackoverflow.com/questions/13167302/did-ie10-change-the-definition-of-window-location-port" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;13167302&#x2F;did-ie10-change-...</a>
stewartjarod超过 11 年前
Just another way IE is a monster ;)<p>I believe Microsoft isn&#x27;t following the same spec that every other browser is using.
rhizome超过 11 年前
Did MS just purchase a new PR campaign contract? There are at least a few stories up front today.
评论 #6529455 未加载
dancecodes超过 11 年前
seems