代码之家  ›  专栏  ›  技术社区  ›  chadley

在src或href中使用“http://”和只使用“//”有什么区别?

  •  11
  • chadley  · 技术社区  · 15 年前

    //somedomain.com/images/img.jpg vs使用 http://somedomain.com/images/img.jpg 其中包括 http:

    两者有区别吗?浏览器只是在纠正缺失的部分吗

    5 回复  |  直到 15 年前
        1
  •  11
  •   rakuo15    15 年前

    如果您已经通过 http ,它将假定您正在谈论 http协议 并通过相同的协议连接到相同的服务器。与相同 https . 如果你在 http协议 https协议 href .

        2
  •  7
  •   Mic    15 年前

    我以前没有见过它,但它是一个有效的URI引用。从语法上说:

    URI-reference = URI | relative-ref
    
    relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
    
    relative-part = "//" authority path-abempty
                 | path-absolute
                 | path-noscheme
                 | path-empty
    

    其中,绝对URI为:

       URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
    

    有关更多信息,请随意阅读本文的其余部分 RFC3986

        3
  •  3
  •   BalusC    15 年前

    http: 在静态内容链接中 <link href> <script src> , <img src> https: 会导致普通网络浏览器弹出安全警告,如以下著名的IE安全警报:

    alt text

    alt text

    通过与方案相关的URL提供“不安全”内容可以解决这个问题。

        4
  •  2
  •   Daniel Vassallo    15 年前

    //somedomain.com/images/img.jpg RFC 3986: Section 4.2 .

    scheme ,因此在http和https之间切换时非常有用,因为您不需要指定方案。

        5
  •  -1
  •   James Curran    15 年前

    我从没见过 //somedomain.com/images/img.jpg 别认为这是合法的。

    我见过 /images/img.jpg 意思是“使用当前域”。如果您有多个网址(如aaaaa.com&aaaaa.net)指向同一个站点。

    推荐文章