Krafton,

>That's exatcly my problem, following your advice, this is my new
>code :
>
>$(document).ready(function()
>     {
>         $("a").bind("click",function()
>              {
>                    var link=$(this).attr("id");
>                    $("#primaryContentContainer").load("pages/"+link
>+".html",function()
>                               {
>                                       $("a").unbind("click");
>                                       $("a").bind("click",function()
>                                               {
>                                                       var
>link=$(this).attr("id");
>                                                       $
>("#primaryContentContainer").load("pages/"+link+".html");
>                                               });
>                               });
>              };
>} );
>
>I had to had an unbind, else my page was loaded twice. But i works the
>way you told me.
>
>What you told about duplicate IDs is right.
>
>If i use href="#dos622" instead of id="dos622" isn't there a risk of
>confusion with anchor?
>
>Can i use rel="dos622" instead of ID or href?
>
>I don't want to use href, because i don't want to intercept external
>links nor anchors in my pages.

You could use "rel" (although some might argue it's not totally semantically
correct) or use the class--although I'd lean towards rel.

Also, instead of unbinding, you can apply the click() to just the new DOM
fragment--no need to reparse the entire DOM:

$("#primaryContentContainer").load(
        "pages/"+link+".html",
        function(){
                var pcc = this;
                $("a", pcc).bind("click",
                        function(){
                                var link=$(this).attr("id");
                                $(pcc).load("pages/"+link+".html");
                        }
                );
        }
);

Actually, you'd be better off just breaking that into a re-usable function:

/* 
        this function will add click handlers for the entire document
        or just a fragment. If no content argument is supplied, run
        run the operation for the entire document object
*/
function addClickHandler(context){
        $("a", context || document).bind(
                "click",
                function (){
                        var link = $(this).attr("id");
                        $("#primaryContentContainer").load(
                                "pages/"+link+".html",
                                function (){
                                        // add clicks to
#primaryContentContainer
                                        addClickHandler(this);
                                }
                        );
                }
        );
}

// add the click handlers
$(document).ready(function(){
        addClickHandler();
});

I didn't test the above code, but it should be pretty close to functional.
The idea is to create a single function you can re-use. By using a "context"
you can run the bind behavior for the entire document or just a DOM
fragment. 

-Dan

Reply via email to