views:

517

answers:

1

Update: This question has been solved, so the buggy Firefox behavior will no longer appear when loading my example webpage

My webpage has very long passages of text, so I want to make each paragraph its own permalink. To do this, I use jQuery to add an anchor and link to each paragraph after the page loads. This worked perfectly in Chrome. To get this to work in IE I added an explicit jQuery focus() function after applying the anchors. However, this does not work in Firefox if I either load the page for the first time or refresh it (e.g. http://www.readsherlock.com/study.php#p4). The relevant javascript is pasted below.

 $(document).ready(function() {
    var i = 1;
    $("#text p").each(function(i){
        $(this).html('<a name="p' + i + '" href="#p' + i++ + '">' + $(this).html() + '</a>');
    });
    if(location.hash != '') 
        $(location.hash).focus();
  });

Thanks!

A: 

This works for me in Firefox 3.0.16

$(document).ready(function() {
    var i = 1;
    $("#text p").each(function(i){
        $(this).html('<a name="p' + i + '" href="#p' + i++ + '">' + $(this).html() + '</a>');
    });
    if(location.hash != '') 
        location.hash = location.hash;
});

I haven't checked any other browsers so you many need to do both the focus and the reset to trigger all browsers.

Rob Van Dam
Thanks this worked! Sometimes the simplest answer is the best.
Sibo Lin