A relative url value for href attribute [duplicate] - html

This question already has answers here:
How do I reload the page without the query parameters?
(14 answers)
Closed 9 years ago.
Let's say currently I'm at www.example.com/category1/?page2, I want to put an anchor tag in the html which could redirect me to www.example.com/category1. I tried to use href='/' but it will take me to www.example.com instead of www.example.com/category1. I also tried to use href=" " but it just refresh the current page. So how should I specify the href attribute of the anchor tag?

One thing you should consider when writing relative URLs is the use of a base element. Base elements allow you to define the the context for a relative URL path.
Information about using the base element: Click Here

if you use a relative url, it's always relative to the top level, not whatever level you're at. So if you want to be at www.example.com/category1, use /category1
EDIT: misunderstood your question. If you want to just clear get params from your url, look at similar questions like How do I reload the page without the query parameters? (looks like using window.pathname is the cleanest solution)

You can use href="../category1/"

As has been noted, if you know the pathname and want to hardcode it into the markup, you can just specify the relative path:
<a title="Go to Category 1" href="/category1">Category 1</a>
Otherwise, if you want a dynamic solution or don't want to hardcode the path, you can use javascript to get rid of the query parameters and make clicking the link do a redirect:
Category N
(For more information on window.location and its properties, see https://developer.mozilla.org/en-US/docs/DOM/window.location#Properties .)

Related

Is it possible to make an href jump on the same page with the destination element not having an id?

Is it possible to make an <a href> link to the same page when your destination doesn't have an ID?
Jump to Topic 1
...
<!--destination is below-->
<h1 class="tab-title" data-editor-style="title" style="color:#444444">Topic 1</h1>
Sadly, the answer is no. You need an ID or at least a name, like CBroe mentioned in a comment of your question.
However, I found this here:
Today, all browsers recognize top as being the top of the page. If you
want the visitor to go to the very top of the page, the ID tag can be
left out and the A HREF link will still work.
A URL fragment identifier can only target an element with a matching ID, or a corresponding named anchor. You cannot link to arbitrary elements without IDs. (There was a proposal that would have allowed the use of CSS selectors as fragment identifiers, in which I was personally involved, but it never took off.)
The best you can do is to use JavaScript to select the element you want and give it an ID that is reserved for your hyperlink. If the elements matching this selector will change then you will additionally have to listen for such changes and handle them accordingly, but that's probably beyond the scope of this question.

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

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?
link to something
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.
If you need to take into account the current path, use the page name directly in the href attribute:
If you are on example.com/news and used an href value of "article", the URL becomes example.com/news/article.
If you need to reference pages on the root directory, precede the page name with slash '/', href="/article".
Make it relative?
link to something
For some browsers/DOCTYPE, you may have to use this in conjunction with the base tag element, which will need to be added to every page that utilises relative paths:
<base href="http://www.example.com/news">

Partial HTML Selection Using Jsoup

So I was wondering if there is a way to find the element that belongs to a specific String that you know exists on a HTML page as part of an attribute. The example is I know that "Apr-16-2015" is somewhere in an attribute on the HTML page. If I go look for it, it's part of the attribute title:
<a title="Apr-16-2015 5:04 AM"
However, I do not have the information about the exact time, i.e. the "5:04 AM". I was wondering if there is a way to partially search an attribute in order for it to return the full element.
This is my code:
org.jsoup.nodes.Element links = lastPage.select("[title=\"Apr-16-2015\"]").first();
Again, it doesn't work because I did not enter the full attribute title, as given above. My question: "Is there any way to make this selector work by not entering the full information, as I will be unable to have the latter part of the attribute to my disposition?"
You can use it in the following way:
lastPage.select("[title^=\"Apr-16-2015\"]").first();
As described on JSoup Documentation:
[attr^=value], [attr$=value], [attr*=value]: elements with attributes
that start with, end with, or contain the value, e.g. [href*=/path/]
References:
http://jsoup.org/cookbook/extracting-data/selector-syntax

Is it possible to link up more than one step with relative paths?

Here is an example of what I'm looking to do:
Link from a page with the path http://mysite.com/lorem/ipsum/ to http://mywebsite.com/ using a relative path.
My first thought was to use this: <a href='.../'>link</a>. But this ends up giving me http://mysite/lorem/ipsum/.../.
Is there a way to do this without calling the actual URL?
How about
link
:)

How to remove #name from URL?

I have a site which has a inner hyperlink http://www.example.com/#name. I want to change it to http://www.example.com.
If it's just in HTML, you need to just simply stop linking to #name and link to the root instead. Go through all your anchor links and remove the reference to #name.
If you have access to dynamic server side languages, you can dynamically filter the content based on a cookie/session/querystring thus removing the need for named anchors.
your question is similar to this stackoverflow.com post:
jQuery removing hash value from URL
java: url.substring(0, url.indexOf("#"));