href to subdirectory of current url - html

Assume the current url is : www.my.com/somewhere.
And I have a href link and want it go to : www.my.com/somewhere/subpage without writing current url(www.my.com/somewhere) in href.
Is it possible in pure HTML (not by javascript or server-side things)?

2020 - current state of this question:
if the current URL is this www.my.com/somewhere
The shorthand ./subpage or just subpage will navigate to www.my.com/subpage
If the current URL ends in a /, then it will navigate as question is asking.
www.my.com/somewhere is not the same as www.my.com/somewhere/
So how to navigate to subpage from www.my.com/somewhere?
use: somewhere/subpage

No, a relative link is relative to your current directory and domain.com/somewhere is a file, while domain.com/somewhere/ is a directory.
Since /somewhere is a file, the current directory becomes '/' and a relative link to 'subpage' becomes a link to 'domain.com/subpage'.
If you used directories for your pages instead of files, then '/somewhere/' would become your current directory. A link to 'subpage/' would now be relative to '/somewhere/' and would open 'www.mydomain.com/somewhere/subpage/'. But this would probably require 'server-side things', and therefor: No, I don't think it's possible to link to a subpage of the current document.

I think ./subpage should do the job.

You can simply write the relative path in your href, like this:
<a href='subpage'></a>

Unless I am misunderstanding you. You should just be able to write href="/subpage".

Related

How do I change the path of a site in HTML, CSS, or JS?

How can I change the path of my site using HTML, CSS, or JS. I may be using the wrong grammar here using the word "path", but what I'm referring to is a subdomain, an example would be stackoverflow.com --> stackoverflow.com/questions
I attempted to use the element
<a href="about.html">
<img src="x">
</a>
but it did not work,
You could think of a server's public folder (the one that host your site's static files as HTML, CSS, Js) as a directory in your computer. For you to redirect the user to another path in your site you can use as you said Your link but keep in mind that if you want to go to another folder like yoursite.com/anotherfolder you must to have two things (for this example):
This folder structure in your site:
[ROOTFOLDER]/anotherfolder/index.html
In this case you need to add another file called index.html because is the default path that you get when no file is specified (file being specified: yoursite.com/anotherfolder/file.html).
The HTML anchor tag must to start with a slash like Your link.
Hope it answer your question.
Note
You are talking about a path http://yoursite.com/[PATH], a subdomain is like this http://subdomain.yoursite.com.

Linking to index.html with slash takes me to my computer's root directory

I am having a simple but frustrating problem. I have a logo on my website that I want to link back to the website's homepage. If i do href="index.html" it works but I when I try to do href="/", which I believe should also work, it takes me to the literal root directory of my computer.
For example, if I have href="/" and then I click on my logo, it directs me here:
Will this be alleviated as soon as it's hosted on a server? Any insight into this behavior would be greatly appreciated.
You're correct, an href of "/" is the root of where the file is being served from, in this case your local file system.
However, I do believe that setting an href of "index.html" or "/index.html" would be slightly more optimized performance, as "/" just resolves to "index.html" anyways.
I would also suggest you set up your local environment to have a local server, where the files are served not just read by your browser. MAMP is an easy way to do this.
Yes, this is a problem that will only occur if you're viewing a site off your filesystem rather than a local or remote server.
If you'd like to prevent this anyway, you could use relative paths rather than absolute. For example:
If you're on /photos/index.html and would like your header logo to go to /, your link could be: ../ to go up one directory.
If you're on /index.html and would like your header logo to point to the current directory, your link could be: ./ to stay in the current folder level.
You can also add this option to disable directory listing and display contents of index.html
Create a .htaccess file with the following:
Options -Indexes
Or you can have this option in the <VirtualHost> directive.
What you wanted to do can be done by :
This will clear the differnce
Further Refrence
https://www.w3schools.com/tags/att_a_href.asp
https://html.com/attributes/a-href/#Different_URL_Forms
This problem was already answered
Base URL - How to call the home link

HTML root folder?

Is there a way to direct a url such as an image location to your root folder and then into a directory from there.
Currently i am using "../" to go back folders but this gets annoying if you have a complex directory tree.
Thanks
It is just a single character:
/
For example:
/image.jpg
whatever page you put it in, it refers to the same file, root_folder/image.jpg
You can also use <base> to set the base url all urls should be relative to. In general using an absolute path with / is probably prefereable, but <base> may be useful if you have the "root" of your page not on the root of the domain. I.E. example.com/mysite/index.html vs. example.com
https://developer.mozilla.org/en-US/docs/HTML/Element/base
You can just use full Url, like:
http://domain.com/file_in_root_folder.jpg
or absolute paths
/file_in_root_folder.jpg
you can use static addressing that start with http:// and for relational addressing you can use your address with / for example: /foo/bar.jpg

HTML: anchor attribute when pointing to index file

Considering leaving the href attribute empty for anchor tags is not a best-practice, how do I go about doing that "legally"?
I don't want to link to index.html or index.php or such, I want to link to the default index file in that document.
Setting it to / does the trick if you are in the root, otherwise it will still go there so it's not a solution.
How should I do this?
Thank you.
If you wish to link to the default document at your current directory, this should be valid:
My Directory's Default Document
If you just wish to have an anchor tag that does not leave the current page:
Go Nowhere
Try linking to ./ - think that should do it
Consider doing it server side.
You can create a function in PHP (causes that's what you are using, right) that would take the current URL and add the current directory and render that.
Do include the domain as they say it is good for SEO.

Urls treated as relative urls when using chrome-extension link?

I use chrome.extension.getURL for a file, and on the page it is placed, it treats it as a relative url (e.g. http://example.com/chrome-extension://ajs8dh8dsfauhdf8auhaffh/blah.js)
How can I make it treat it as an absolute URL instead? It is placed into the href component of a tag.
EDIT: I've seen people's plugins do this for CSS, so I know it is possible. Maybe not for href attributes?
chrome.extension.getURL should return a URL that starts with chrome-extension://. For example if you did chrome.extension.getURL("blah.js"); the value returned would be similar to "chrome-extension://ajs8dh8dsfauhdf8auhaffh/blah.js". This URL points to a local file stored in the extension's directory that is created when the extension is installed. The "ajs8dh8dsfauhdf8auhaffh" is a hash representing your extension to chrome. You're obviously getting something close to that, but the question is why is your's prefixed with "http://example.com/". I would check how you are setting the href attribute.