开发者

How do browsers interpret hrefs that start with "http:/"?

Some of my users are creating links that look like

<a href='http:/some_local_path'>whatever</a>

I've noticed that Firefox interprets this as

<a href='/some_local_path'>whatever</a>

Can I count on this occurring in all browsers? Or should开发者_JAVA百科 I remove the http:/s myself?


This is an unusual URL, but is not invalid. The URL spec says that omitted components are defaulted from the base url, which can be provided explicitly in a <base> tag, or absent that, the current URL of the page.

When a browser sees /some_local_path, it is missing a scheme and a host, so it takes them from the base url. When your users put http:/some_local_path, it has an explicit scheme, but is missing a host, so the host defaults to the base url. If your page is an http: page, then the two URLs will be interpreted identically.

All that said, those URLs are almost certainly not what your users intended. You'll be helping them if you point out their error.


It's always best to validate data entered by users. Inevitably, you'll get something unexpected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜