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.
Related
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.
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".
I am currently designing a small website using JSP / Servlets. Tomcat is very picky about my routes.
My application can be entered throught the path /Final (on a localhost I have http://localhost:8088/Final).
My main JSP page is at http://localhost:8088/index.jsp. On that page, I have a navbar with several links. On one of those links, the "Home" link, I have the href set to /Final/index.jsp. When I hover over the link, it shows the links path as http://localhost:8088/Final/Final/index.jsp.
At first, I said ok, lets just change the href to index.jsp, but when I do that, the link now points to http://localhost:8088/index.jsp which will not satisfy the server. Why are links behaving like this? How can I get it to point to http://localhost:8088/Final/index.jsp without fully qualifying it?
You could use a relative path such as pointing it directly to it's location. I'm not sure if this would work, but try putting /index.jsp as the href in your link. This way it's going from your current folder "Final" and going one level down to index.jsp.
Apparently, adding the . before the path as in ./index.jsp works just fine.
Also, you can try to escape your file and specify it again, it worked for me.
For example: ../jsp/index.html
How does an anchor tag work that does not specify a filename?
Example:
Fast Cars
I was under the impression the HTML file had to be expressly cited, but in this case it is not, just the directory name.
Does this reference an HTML file, or is it something else?
Depending on the webserver configuration, pointing to a directory might mean pointing to the index file of that folder, or even at whatever other file you want to rewrite.
There was a time in which SEO practices reccomended using folder style permalinks instead of query strings. So there were cases in which
/index.php?section=fastcars
was rewritten as
/fastcars
Again, this is all rewrite magic. Most webservers offer some kind of rewrite rules to achieve this.
Besides, an a element doesn't need to point to a file or url address. They can be used as internal navigation links to point to an anchor, and JS frameworks such as jQuery have popularized its use only as button replacements to trigger a given behavior.
An anchor tag's href attribute does not reference a file; it references a URL. What that URI corresponds to is completely opaque. In the most common case where the URL uses the HTTP(S) protocol, that URL is sent to the server as part of a request.
It's the server, and only the server, that determines what the URL corresponds to. It doesn't have to be a file or anything else that you might consider tangible, and you can't tell looking at it from the client side.
The main function of the "a" attribute is to create links. That means a link to an external website:
<a href="https://www.stackoverflow.com">
or a link within the same page. Just create an id - for example:
<h2 id="top">text text text</h2>
then create a link using that id -
Top
Every time you click on the link, your browser will send you to the part of the page where your id is.
If my path is "http://www.example.com/folder1/folder2/" how can I return to "http://www.example.com/folder1/"
I've tried
Back
Which get me back to "http://www.example.com/" and not the previous folder. Any clue on how to do it ?
EDIT
I have a
<base href="http://www.example.com/" />
in the head, could it be because it try to go down 1 folder from that base ?
Back
Should work if you drop the trailing slash. Don't really know why it wouldn't work with it but if it's not working, try that...
EDIT Just tested and both versions worked for me in IE8 - what browser are you using to test it?
Another Edit
If you use a <base> tag, all relative hrefs will be relative the href attribute specified as the <base> (thats the point of it). If your base is the root of the site, like http://www.example.com/, links that try to step down a directory won't make any sense, since there's nowhere lower to go, and they will just point to the root directory. However, if the base is the root of the site, there's probably no point in having one at all, since this is, effectively, the default - it would only make a difference if you were working from a higher level directory.
If the base is not the root, but somewhere higher (like http://www.example.com/somedirectory/) there is a point in the base declaration, but you have to make a decision - something would have to be specified absolutely. So if you have done it because you want to refer to all your images as just file.jpg instead of /somedirectory/file.jpg, you either need to change your image references to the absolute /somedirectory/file.jpg, or have your 'navigation links' like the one shown in the question specified absolutely. You can't do both.
I would say (although I don't know much about the rest of your site and how it was built) that your best bet is to scrap the <base> in order to allow for relative navigation links, but the choice is yours...