tags:

views:

114

answers:

3

I'm creating a "Q & A" website. On the questions page all of its answers could be seen (with pagination). I need to go straight down to answers (in the page) from other pages as well. I've tried implementing named anchors but unable to direct to page section.

Answer's get_absolute_url() returns url in the form:

www.example.com/question-id/question-slug?page=no#aID <!-- EDITED -->

An example url:

www.example.com/question-10/what-is-this?page=2#a20   <!-- EDITED -->

and in html:

    {% autopaginate answers 10 %}
    {% for answer in answers %}
      <div>
        <a name="a{{answer.id}}">Answer</a> <!-- EDITED -->
        {# answer body goes here#}
      </div>
    {% endfor %}
    {% paginate %}

It redirects to the correct page but not to the correct page section.

EDIT: Result! When clicked, After going to the page section, it bounces to the bottom of the page

+1  A: 
a_{{answer.is}}

may be you have a typo - iD ?

a_{{answer.id}}
Pydev UA
corrected it.But in my code it is {{answer.id}}
anand
@anand: Drat, it was such a nice simple answer. :-)
T.J. Crowder
+5  A: 

A surprisingly little-known fact is that anchors can go to any element with an ID on the page; it doesn't have to be an anchor element. So you might just put the ID on your div:

<div id="a_{{answer.id}}">
    <span>Answer</span>
    {# answer body goes here#}
</div>

(Obviously, use whatever element makes sense around the answer header; the span above is just for illustration.)

As far as I know, there's nothing wrong with using anchors (although anchors with content tend to exhibit link-like behaviors -- rollovers and such -- if you're not careful with your CSS), but just as an option, you have this other alternative as well.

T.J. Crowder
good to know this. How does *stackoverflow* does it. It seem from the **permalink** url that each answer has its own url
anand
@anand: Nothing magic about what they're doing. It's basically a REST URL in the format {server}/questions/{questionId}/{ignoredDashedTitle}/{answerId}. Note that {ignoredDashedTitle} -- since the title can change, SO ignores it entirely and works just by the question ID and answer ID (2171237 and 2171265, in this case). You could put anything in the ignored title area. Then when scrolling to an answer they add #{answerId} to the URL so the browser scrolls down. They seem to be using actual anchors for this (`<a name="2171237">`). I wonder if there's an accessibility or TOC reason for that.
T.J. Crowder
@anand: (cont'd): Example of how they ignore the title part of the URL: http://stackoverflow.com/questions/2171237/blah-blah-blah/2171265#2171265
T.J. Crowder
+3  A: 
http://www.example.com/question-10/what-is-this#a_20/?page=2
<a id="a_{{answer.id}}">Answer</a>

There is no mechanism of path parts (/) or query strings (?) in a fragment identifier.

Is the pagination parameter supposed to be sent to the server-side? If so, that's an easy fix:

http://www.example.com/question-10/what-is-this?page=2#a_20

If not; if you're doing client-side pagination, which is why the query was appended as part of the fragment identifier, then you've got trouble.

You can't link to part of a document and include extra parameters for client-side-scripted pagination: you can have a fragment identifier that points to a real identified element on the page, or a fragment identifier being used as a hack to pass parameters to script, but you can't have both at once. The fragment you have specified is a single long string, and can only be matched by:

<a name="a_20/?page=2">foo</a>

(I used the old-fashioned name attribute here instead of the usually-preferred id attribute because this:

<div id="a_20/?page=2">foo</div>

is invalid. id attributes cannot include arbitrary punctuation characters; they are NAME tokens which may only be alphanumerics, _, ., - and : (though the latter is inadvisable). If you wanted to specify other types of character they would have to be encoded in some application-specific way.)

bobince
Ok I 'm going to use this : http://www.example.com/question-10/what-is-this#a20/?page=2and since I'm going to use strict XHTML someday I dont't want to use **name** attribute.
anand
yes mine is server side pagination
anand
`<a name>` is perfectly valid in XHTML 1.0 Strict. `id` may normally be preferred, but `name` is not deprecated until HTML5.
bobince
Ah! If it's server-side pagination there's no problem then. You just need to put the fragment after the query string.
bobince
I edited the question, check it out!
anand
erm... the edited version doesn't really change anything. You've still got the `?page=n` query after the `#`... you can't be doing server-side pagination if that actually works, because the fragment identifier is never passed to the server. `#...?...` can only work if there's really an `id`/`name` with that exact entire string, or if you have some JavaScript in there that decodes the queried fragment, extracts the id and manually scrolls to it.
bobince
I've **corrected it**. but `After going to the page section, it bounces to the bottom of the page`
anand