javascript - Creating links without actually putting the whole link in <a> tag


I've been wondering how do other websites do their navigation links. What most beginners like me do is put the whole link inside the tag

<a href="">News</a>

or something like this:

<a href="news.html">News</a>

But I've seen a lot of websites lately that does this:

<a href="/news.html">News</a> 

and then the browser address bar display it like this

First I want to know how do you call this method? and what are the advantages of doing this. and lastly, how does it actually work?.

I want to research about it but I don't know what to type on google. I did try a lot of keywords I could think of that relates to this but nothing comes close to what I'm looking for




There's 4 basic link types:

self-link:href="". This is a short-hand form of "link to yourself". You'll see it used for in-page anchors, such ashref="#top" and the link.

relative:href="news.html". Clicking on this will try to load anews.html page in the SAME directory as the page that the link is contained in, so if you're on, you'll be trying to load

local absolute:href="/news.html". This will try to load anews.html page from the document root of the site. If you're on, you'll be trying to load The leading/ is what makes this a local absolute instead of a relative path.

full absolute:href="". A full-blown url, which can point to a completely different site if need be. It CAN be point to the exact same site you're on, but essentially it's a "go over there, no matter where over there is".




This has to do with HTML, not JavaScript. Your first snippet uses an absolute path. The second two use relative paths. For more information, read this:




The best practice (for me?) is the first one, you need to write all the URL for the most readable code..

The first and the 3rd link are the same if you are on the same domain name..

The 2nd is different, if you are on you'll go to But if you are on you'll be redirected to and not on root directory.

But I don't understand why you talk about /news/ directory ?




Relative URL's will probably get you some results.

  • news.html will navigate relative to your current location
  • /news.html will navigate relative to the root domain name

For example if your current location is :

href="news.html" will get you to


href="/news.html" will get you to

See also

People are also looking for solutions to the problem: php - How can I add rows to this HTML table dynamically...?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.