1

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?

chaonextdoor
  • 5,019
  • 15
  • 44
  • 61

4 Answers4

2

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)

Community
  • 1
  • 1
Colleen
  • 23,899
  • 12
  • 45
  • 75
  • This wouldn't work for the general case: `www.example.com/some/other/stuff/categoryN?foo` – Noyo Mar 04 '13 at 21:38
  • Thanks, man. But what I want to know is if there is some way that I don't need to hardcoded the relative url and it just goes to the url without everything after question mark? – chaonextdoor Mar 04 '13 at 21:43
  • @Noyo what are you talking about? Why not? – Colleen Mar 04 '13 at 21:48
  • As @chaonextdoor commented, (s)he(?) wants a general solution without having to hardcode the path part. My solution fixes this with a little javascript. – Noyo Mar 04 '13 at 21:50
  • 1
    ah, yes, that is true. Except in that case, this question is a duplicate. – Colleen Mar 04 '13 at 21:51
2

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

theaccordance
  • 889
  • 5
  • 13
0

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

MIIB
  • 1,849
  • 10
  • 21
-1

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:

<a href="#" title="Return to main category page" onclick="window.location.href = window.location.pathname; return false;">Category N</a>

(For more information on window.location and its properties, see https://developer.mozilla.org/en-US/docs/DOM/window.location#Properties .)

Noyo
  • 4,874
  • 4
  • 39
  • 41