0

My website code sample:

<a href=/post/64/page-name><img src=/img-folder/2015/09/image.jpg></a>
<div id=cont2><a href=/post/64/page2>page 2 link</a></div>

My first question is, can I start links just with /? Is it a bad practise? because all website sources that I looked it starts with www.website.com/... not just /

Second question is quotes. It is not needed since html 2.0, but is it important in the example above?

My website is having some problems on google to show correctly... may it be because this problems?

martin jakubik
  • 4,168
  • 5
  • 29
  • 42
Joe Silva
  • 1
  • 3

7 Answers7

2

It isn't bad practice. A URL starting with / is merely a relative URL that's relative the the base path. You're using it just fine.

Another example usage is when you want to reference a CSS or JavaScript file and you're deep down into the path.

<script src="/scripts/main.js"></script>

Then, no matter where the user is on your site, they'd always request http://example.com/scripts/main.js. Where example.com is your site's domain.


Additionally: Always quote attribute values. (attribute="value" and not attribute=value).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1
  1. / means start of where you are currently. So if your resource is located under same directory, you are allowed to use / to start with. If you refer to external resources, you can't use / to start. (E.g. www.google.com means google website, while /www.google.com means folder under your current directory named www.google.com, like http://localhost/www.google.com)

  2. Quotes are needed when you use white-space in your attributes values (e.g. class="my super classs-name that has white-space" | class=my super classs-name that has white-space).

  3. My website is having some problems on google to show correctly SEO stuff. What problems? Your page is not under first page of Google search? It's separate topic about that.

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

if you reference external sources you have to add the full path/adress
if you reference local resources its up to you.(more or less)
take a llok at How to properly reference local resources in HTML?
You should use either double " or single ' qoutations - thats a good practise at least.
But you dont have to if there is no whitespace.

Community
  • 1
  • 1
Minzkraut
  • 2,149
  • 25
  • 31
0

It is not forbidden. When you start your link with slash / it just a path relative to base element.

You can read more about BASE element here: http://www.w3.org/TR/html4/struct/links.html#h-12.4


For example, if you are already at: http://example.com/folder/index.html

/posts/index.html would link to: http://www.example.com/posts/index.html posts/idnex.html would link to http://www.example.com/folder/posts/index.html

mtszkw
  • 2,717
  • 2
  • 17
  • 31
0

When you start your link with "/" its mean that you start from the root directory. Example: Your website is in the directory /web/html. When you now start your link with "/" its goes to the root folder. In this case the web folder.

0

I know this is old and answered, but it came up on Google when I was searching for something similar, so I just wanted to add to the answers.

Sometimes, when I need to do something real quick with simple HTML site that doesn't require a server, I usually just open index.html in Terminal to quickly preview the page in browser. However when you open your site like that, using the leading slash to load resources (ie. /js/main.js) won't work. That's because when you load your website by opening a file in your browser, the browser takes the root of your drive as the base path for your website.

So if you have your files like this for instance:

drive/Users/username/Documents/www/index.html
drive/Users/username/Documents/www/js/main.js

And you reference your script like this:

<script src="/js/main.js"></script>

The browser will think you're actually pointing here (if you open the file directly in browser):

drive/js/main.js

Because / in this case means drive and not the website's root (www in this case) folder as it would on a server.

-1

Nope, it is not a bad habit to put '/' when starting links. But not having a quote in every html attribute? I don't think so. But i would suggest to put quote(") in every html attribute for it to be more readable.

Makudex
  • 1,042
  • 4
  • 15
  • 40