Google Answers Logo
View Question
Q: Javascript Table Sorting ( Answered 5 out of 5 stars,   1 Comment )
Subject: Javascript Table Sorting
Category: Computers > Programming
Asked by: trevorturk-ga
List Price: $50.00
Posted: 21 May 2005 06:06 PDT
Expires: 20 Jun 2005 06:06 PDT
Question ID: 524043
I would like to have the fix(es) to get this Javascript Table Sorting
working properly in Safari:
Subject: Re: Javascript Table Sorting
Answered By: wildeeo-ga on 21 May 2005 10:52 PDT
Rated:5 out of 5 stars
Hi, trevorturk. Thanks for your question.

The reason the script you linked to doesn't work in Safari is because
Safari's support for the cellIndex property of cells is broken - it
always returns '0', no matter which cell you click on. This means it
will always try to sort the first column, no matter which one you
click on.

Fortunately, the fix is fairly simple, if a little less neat than the
original solution. You can make it work correctly in Safari by making
the following changes:

Change the line:

cell.innerHTML = '<a href="#" class="sortheader"
onclick="ts_resortTable(this);return false;">'+txt+'<span


cell.innerHTML = '<a href="#" class="sortheader"
onclick="ts_resortTable(this, '+i+');return false;">'+txt+'<span

Change the line:

function ts_resortTable(lnk) {


function ts_resortTable(lnk, clid) {

And change the line:

var column = td.cellIndex;


var column = clid || td.cellIndex;

There's an example of the patched code at and you can download an
already-modified copy from

If you have any questions, please feel free to request a clarification.


Request for Answer Clarification by trevorturk-ga on 22 May 2005 09:17 PDT
I'm sorry that I can't test this myself, but your fixed script will
still work in the other browsers? What I mean is, the changes you made
don't affect the functionality on Internet Explorer?

Clarification of Answer by wildeeo-ga on 22 May 2005 10:30 PDT

I've tested it myself in Safari 2.0, Firefox 1.0.4, and (Windows)
Internet Explorer 6. It worked without any problems.

It's a fairly simple change (adding one additional variable to a
function), so problems shouldn't occur.

Request for Answer Clarification by trevorturk-ga on 22 May 2005 16:35 PDT
Please note this related question:

Clarification of Answer by wildeeo-ga on 23 May 2005 09:14 PDT
I'll have a look at it. :-)
trevorturk-ga rated this answer:5 out of 5 stars
very fast answer, just what i needed, saved me a ton of time, thanks

Subject: Re: Javascript Table Sorting
From: rs3-ga on 27 Apr 2006 10:25 PDT
Thanks very much - it was what i was looking for.  I have one problem
though - when the page refreshes, the sort column is lost.  Is there a
way to remember the column the table was last sorted on?

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy