Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an anchor element's href relative to the current URI?

Tags:

html

Im on the page:

example.com/news

It's a list of news articles. Is it possible to set links on each news article and take into account the current url?

<a href="the-article">link to something</a>

The above will link to:

example.com/news/article

Or will I need to get the entire route and specify that in the link?

The url could be anything eg. /products so I do not want to hardcode it in.

like image 429
panthro Avatar asked Oct 23 '25 23:10

panthro


1 Answers

Browser replaces the part comes after the last / of open page's URL to produce absolute URL from relative path. Either:

  • Use filenames at the end of your URLs (like news/index.html)
  • Use / at the end of your URLs (like news/)
  • Use absolute URLs instead of relative paths for href (like example.com/news/the-article)
  • Set a base element to guide the browser to add last /
    <base href="http://example.com/news/the-article/" />
    
like image 175
ufukty Avatar answered Oct 25 '25 19:10

ufukty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!