Href without http(s) prefix

后端 未结 4 1615
死守一世寂寞
死守一世寂寞 2020-12-29 21:22

I just have created primitive html page. Here it is: example And here is its markup:

www.google.com

&l
相关标签:
4条回答
  • 2020-12-29 22:03

    It's possible, and indeed you're doing it right now. It just doesn't do what you think it does.

    Consider what the browser does when you link to this:

    href="index.html"
    

    What then would it do when you link to this?:

    href="index.com"
    

    Or this?:

    href="www.html"
    

    Or?:

    href="www.index.com.html"
    

    The browser doesn't know what you meant, it only knows what you told it. Without the prefix, it's going to follow the standard for the current HTTP address. The prefix is what tells it that it needs to start at a new root address entirely.

    Note that you don't need the http: part, you can do this:

    href="//www.google.com"
    

    The browser will use whatever the current protocol is (http, https, etc.) but the // tells it that this is a new root address.

    0 讨论(0)
  • 2020-12-29 22:03

    I've created a little function in React project that could help you:

    const getClickableLink = link => {
        return link.startsWith("http://") || link.startsWith("https://") ?
          link
          : `http://${link}`;
      };
    

    And you can implement it like this:

    const link = "google.com";
    <a href={getClickableLink(link)}>{link}</a>
    
    0 讨论(0)
  • 2020-12-29 22:07

    You can omit the protocol by using // in front of the path. Here is an example:

    <a href="//www.google.com">Google</a>
    

    By using //, you can tell the browser that this is actually a new (full) link, and not a relative one (relative to your current link).

    0 讨论(0)
  • 2020-12-29 22:10

    Omitting the the protocol by just using // in front of the path is a very bad idea in term of SEO. Ok, most of the modern browsers will work fine. On the other hand, most of the robots will get in trouble scanning your site. Masjestic will not count the flow from those links. Audit tools, like SEMrush, will not be able to perform their jobs

    0 讨论(0)
提交回复
热议问题