problem in recognizing href as link

Asked

Viewed 218 times

1

I have a problem that my href does not recognize the link coming from the database.

For example if I put www.facebook.com in the database, when I call it inside the href he brings me href="localhost/projeto/www.facebook.com", but if I take the facebook link https://www.facebook.com/ and put in the database when I call on href he returns to me like this href="https://www.facebook.com/" In other words, it returns the correct link. someone knows what I have to do to href recognize www.facebook.com as a link?

  • 1

    I think your problem is the lack of protocol http://, https:// in front of the link probably your link this way <a href='facebook.com'> should be <a href='https://facebook.com'>

  • then but then and that’s not me who will manage it I have no way of knowing if the person who will administer will use the protocol, why it would be simple to just put the https:// before the content she types.

  • 1

    It is up to the programmer to process the data sent via input and to define if what the user typed is correct! the problem is probably what @Icaromartins mentioned, I would try to treat this input to not let you save the url without http!

  • I think it would be good for you to treat this on the server side by checking whether the user will type http or not when saving.

  • Knowing that it will be a link, in reading the data (before putting the a href) Voce can remove the http:// or https:// and put only the // as in the example <a href="//facebook.com"> if I’m not mistaken this should work for a href also, it will automatically select the protocol based on the current page

  • understood did not know that only the // worked I always thought I should specify.

Show 1 more comment

1 answer

1


Ideal and you treat before you insert in your database, however if you know that it will always be an external link you can remove the http:// and https:// when you have, and use the // as shown in the example below. It will behave automatically based on the current page, ie if you are on the protocol http:// he’s gonna use http:// if Voce is under protocol https:// he will use https://.

<a href='//facebook.com' target='_blank'>Facebook</a>

Possible problem: If Voce is up the protocol https:// it may be that the destination page does not have this implemented.

Another solution and you always remove http:// and https:// and put by default the http://.
  • if it is not only with external link this is not right either?

  • Only if the link has the dominio example //meusite.com/pagina2.html

  • 1

    so sure I’ve done here I’ve tried it too

Browser other questions tagged

You are not signed in. Login or sign up in order to post.